2014-12-02 63 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

相關問題