2

I have the following code on a web page:

var date = new Date(row.EventDate.replace('T', ' ')); // 'T' comes from sql server
var month = date.getMonth() + 1;
var day = date.getDate();
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
return '<span>' +
    (month > 9 ? month : '0' + month) +
    '/' +
    (day > 9 ? day : '0' + day) +
    '/' +
    year +
    ' ' +
    (hours > 9 ? hours : '0' + hours) +
    ':' +
    (minutes > 9 ? minutes : '0' + minutes) +
    '</span>';

Chome displays as 02/27/2017 13:30 as expected, but IE shows NaN/NaN/NaN NaN:NaN

I have read several posts about issues setting a date field having issues like this, but I am setting a text string.

Where am I having the disconnect as to why IE is complaining about something not being a number?

Keith Clark
  • 609
  • 1
  • 7
  • 19

1 Answers1

0

IE has struggeled with this for a while. The following formats will work in most (all) modern browsers. Change your row.EventDate model output to match one of the following formats and it should work just fine.

Source: http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari/

var d = new Date(2011, 01, 07); // yyyy, mm-1, dd
var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss
var d = new Date("02/07/2011"); // "mm/dd/yyyy"
var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss"
var d = new Date(1297076700000); // milliseconds
var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC
Rick Burns
  • 1,538
  • 16
  • 20
  • The only format that's cross-browser is ISO 8601 format (i.e., "2011-02-07T11:05:00Z") – Heretic Monkey Feb 27 '17 at 20:00
  • OMG! Thanks to Mike's response, I tried removing the ```.replace('T',' ')``` from my code changing the feed to ```new Date()``` from ```2017-01-01 00:00:00``` to ```2017-01-01T00:00:00``` add it works perfect !!!! – Keith Clark Feb 27 '17 at 20:07
  • @RickBurns Did you see the version numbers of the browsers on that reference? Things have changed since version 10 of Chrome... – Heretic Monkey Feb 27 '17 at 20:10
  • @MikeMcCaughan, exactly my point. This example from version 10 of Chrome still stands today as cross-browser supported date formats. Not just ISO 8601. – Rick Burns Feb 27 '17 at 20:12
  • Hey, if you want to depend on those formats never changing, rock on, but the spec only defines ISO 8601. – Heretic Monkey Feb 27 '17 at 20:13
  • Spoke too soon. Grrr... Leaving ```T``` in no resolves to Z time instead of locale time. – Keith Clark Feb 27 '17 at 20:15
  • @KeithClark, if you specify the timezone offset instead of Z (no offset), or convert to Z time before javascript parsing, it should output as you want. – Rick Burns Feb 27 '17 at 20:18
  • @KeithClark, I should say if you use ISO 8601 as mentioned in the comments, the default is Z time, if you specify a format as provided in the answer, it will assume local time. – Rick Burns Feb 27 '17 at 20:20