2014-12-22 156 views
0

我一直在尋找這個非常具體的轉換,我無法找到任何地方javascript日期轉換日期無效

var d = new Date("2014-12-25T18:30:00+0100"); 
console.log(d.toString()); 

中的console.log返回「無效的日期」 的DateString是由Facebook的GraphAPI返回。

我在做什麼錯?誰能幫忙?

在此先感謝

編輯:

現在我固定的API我的輸出是一種consfusing的:

我試圖分裂字符串

d.getDay()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes(); 

它輸出

4.11.114 18:30 

爲什麼?!

+0

其他地方還有其他代碼嗎?這在FF和Chrome中似乎都能正常工作。 – brbcoding

+0

WHAT瀏覽器???我的猜測IE瀏覽器 – epascarello

+0

http://stackoverflow.com/questions/5802461/javascript-which-browsers-support-parsing-of-iso-8601-date-string-with-date-par – epascarello

回答

0

而不是做那些複雜的日期函數

d.getDate()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes(); 

請你幫個忙,並在項目中包含http://momentjs.com/。然後,您可以簡單地採取從Facebook的API的日期和與

moment("2014-12-25T18:30:00+0100").format("/* date format */"); 

See here for formating

阿里納斯格式化

當在格式化純JavaScript的日期,你將有1個月添加到您的一個月 - 一月份爲0,這就是爲什麼你會得到4.11 ......而不是4.12 ...

+0

請將d.getDay()更正爲返回正確數字的「d.getDate()」 - 然後您的答案完全正確:) –

+0

@MaxBumaye :)謝謝! – baao

0

更改了getYear()來調用getFullYear()

d.getDay()+'.'+d.getMonth()+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes();