好吧,我有這個確切日期:JavaScript格式UTC日期
2012-10-01T13:00:00+0000
我需要把它分成兩個變量:一個是日期:2012-10-01,另一種是在時間小時和分鐘:13:00
我迄今產生2012-09-12 & 14:00 - 完全錯誤的日期...
下面的代碼:
var d, dd, hh, mi, mm, theDate, theTime, yyyy;
var myDate = "2012-10-01T13:00:00+0000"; //Date I need converting into two variables
d = new Date(myDate);
yyyy = d.getFullYear().toString();
mm = d.getMonth().toString();
dd = d.getDate().toString();
hh = d.getHours().toString();
mi = d.getMinutes().toString();
theDate = yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]);
theTime = (hh[1] ? hh : "0" + hh[0]) + ":" + (mi[1] ? mi : "0" + mi[0]);
//theDate produces: 2012-09-12, (should be 2012-10-01)
//theTime produces: 14:00, (should be 13:00)
使用getUTCFullYear,getUTCMonth,getUTCDate,getUTCHours和getUTCMinutes。並且記住那個月總是0-11。 – some