2014-12-02 52 views
-1

你能告诉我为什么这不适用于Firefox(V 34 latest)吗?它在所有其他浏览器上工作正常。 'DatePosted'显示为无效日期。为什么?任何帮助将不胜感激。新日期不适用于Firefox

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

enter image description here

注: x.DatePosted: “2014-11-18T08:06:39.06”

+0

[jQuery的日期转换铬的作品,但IE和Firefox不(可能重复http://stackoverflow.com/questions/9595902/ jquery-date-conversion-chrome-works-but-ie-and-firefox-dont)或http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok – CBroe 2014-12-02 17:43:47

+3

Can你请发布'x.DatePosted.toString()'的值? – 2014-12-02 17:44:02

+0

@RahulDesai就像这样:“2014-11-18T08:06:39.06” – Sampath 2014-12-02 17:46:35

回答

2

你不需要更换T。它没有它(Chrome和Firefox测试)。

设置Date对象后,将其设置为UTC。

工作如下片段:

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

+0

是的。我用了没有更换部分,现在它的工作。谢谢很多:) – Sampath 2014-12-02 18:10:39