Cross Browser date wackiness

1551
4
08-17-2012 10:28 AM
SteveCole
Frequent Contributor
On the page I've been developing, I've been populating a datagrid with attributes from a feature layer. A few of the attributes are date fields. I found out the hard way about the the format of date values returned through the API so I looked for a function to properly format my dates and times back into xx/xx/xxxx and xx:xx ampm formats.

I settled on the following functions which use dojo:

function formatDate(value){
 var inputDate = new Date(value);
 return dojo.date.locale.format(inputDate, {
   selector: 'date',
   datePattern: 'MM/dd/yyyy' 
 });
}

function formatTime(value){
 var inputTime = new Date(value);
 return dojo.date.locale.format(inputTime, {
   selector: 'date',
   datePattern: 'K:mm a' 
 });
}


The routines are used in a formatter() function for one of my columns in the datagrid like this:

var rowdata = this.grid.getItem(rowIndex);
var theClosureDate = rowdata.dateCl;

alert(formatDate(theClosureDate));


Here's where it gets weird- I've been doing my development and debugging with Firefox and the dates displayed are correct. I just loaded the page in Chrome and IE-8 and the dates displayed are "NaN".

Does anyone have a conversion function they can share which works across all browsers? It would be especially great if it also handled the UTC/local time difference. I realized that these routines don't.

Thank you!
Steve
0 Kudos
4 Replies
ReneRubalcava
Frequent Contributor
Dates are my kryptonite, but to I've had really good luck with moment.js
http://momentjs.com/
I was having issues between IE and Chrome parsing dates, this little lib helped simplify the process.
The draw for me was it could easily handle asp.net json dates. Maybe it could help you out.
0 Kudos
SteveCole
Frequent Contributor
Thanks Rene for the the link to moment.

I'm feeling a bit dense but I can't seem to get it to work with the dates from my data. The dates are being returned through a query so the "format" of the information coming back is a series of numbers such as "1300406400000". I tried using moment like this but it doesn't work:

var temp = moment(new date(theClosureDate));
alert(temp.format('M/DD/YYYY'));
0 Kudos
ReneRubalcava
Frequent Contributor
try something like this
var m = moment(1300406400000);
console.log(m.toDate());
// result is Thu Mar 17 2011 17:00:00 GMT-0700 (Pacific Daylight Time)
console.log(m.format('M/DD/YYYY'));
// result is "3/17/2011"
0 Kudos
SteveCole
Frequent Contributor
Well HALLELUJAH! I have solved the mystery of date formatting!

Your suggestion, Rene, still didn't work for me so I kept playing with it. If I manually typed things in within the javascript console, it would work. If I used the same thing within my javascript function, it would not. This got me thinking that perhaps the date returned via the query was a string instead of a number.

To test it, I tried adding the "parseInt" function:

alert(new Date(parseInt(rowdata.dateCl)));


Lo and behold- it now works in Chrome and STILL works in Firefox. Thank god! That was maddening. Thanks again, Rene for helping me out!
0 Kudos