PDA

View Full Version : Formatting a date from European format to longhand


jdubois
September 20th, 2010, 11:26 AM
I was attempting to use the following code to reformat a date into longhand, but received an error which I believe is the result of the incoming data being in a european style.

The code I am using is:
return FormatDate(Field("UPLOAD_DATE"), "ld, lm d, 20yy");

The Error I am receiving is:
uncaught exception: Error in function FormatDate: The input string is invalid

The input string fromt he data source comes over as 8-Sep-10, and I would like it to output as September, 8 2010.

Any help on what I am doing incorrectly or how I could accomplish this is greatly appreciated.

Jerimiah DuBois

Dan Korn
September 20th, 2010, 03:40 PM
JavaScript only parses dates in very specific formats; European date formats generally are not among them. You need a custom date parser for those. Try this:
var str = Field("UPLOAD_DATE");

var MonthNameAbbrs = [ "jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec" ];

var month_name_part = 0;
var parts = str.replace(/-/g,'/').split('/');
for (var i = 0; !month_name_part && i < parts.length; i++)
{
if (isNaN(parseInt(parts[i])))
{
for (var m = 0; m < MonthNameAbbrs.length; m++)
{
if (ToLower(parts[i]).substr(0,3) == MonthNameAbbrs[m])
{
parts[i] = m + 1;
month_name_part = i + i;
break;
}
}
}
}

if (month_name_part)
{
var mpart = month_name_part - 1;
var mval = parts[mpart];
parts.splice(mpart, 1);
parts.splice(0, 0, mval);
}

var date = DateFromString(parts.join('/'));
return FormatDate(date, "ld, lm d, yyyy");