-1

Can you tell me why this is not working on Firefox (V 34 latest) ? It's working fine on all other browsers. 'DatePosted' is shown as Invalid Date.Why ? Any help would be highly appreciated.

 //Get local time for everything:
       data.Comments.forEach(function (x) {
         x.DatePosted = new Date(x.DatePosted.toString().replace("T", " ") + " UTC");
      });

enter image description here

Note : x.DatePosted : "2014-11-18T08:06:39.06"

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • possible duplicate of [jquery date conversion chrome works but IE and firefox don't](http://stackoverflow.com/questions/9595902/jquery-date-conversion-chrome-works-but-ie-and-firefox-dont) or http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok – CBroe Dec 02 '14 at 17:43
  • 3
    Can you please post the value of `x.DatePosted.toString()` ? – Rahul Desai Dec 02 '14 at 17:44
  • @RahulDesai It's like this : "2014-11-18T08:06:39.06" – Sampath Dec 02 '14 at 17:46
  • 1
    why do you need the .replace("T"," ") inside the constructor call of Date? It should work with-out it. – AWolf Dec 02 '14 at 17:49
  • @Sampath Please checkout the answer below. – Rahul Desai Dec 02 '14 at 18:10

1 Answers1

2

You dont need to replace the T. It works without it (tested in Chrome and Firefox).

After setting the Date object, get it into UTC.

Working snippet below:

var myDate = new Date("2014-11-18T08:06:39.06");

// now set it to UTC
var myDateinUTC = Date.UTC(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), myDate.getHours(), myDate.getMinutes(), myDate.getSeconds(), myDate.getMilliseconds());

console.dir(myDateinUTC);

var myNewDate = new Date(myDateinUTC);

console.log(myNewDate.getMonth()); // just to test
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138