0
我的日期格式爲2011-04-01
。當我運行下面的代碼時,會得到以下結果。js日期對象產生奇怪的結果
var dateParts = '2011-04-01'.split('-');
dateParts[0] = parseInt(dateParts[0]);
dateParts[1] = parseInt(dateParts[1]);
dateParts[2] = parseInt(dateParts[2]);
console.log(dateParts);
for (i = 0; i <= 13; i++) {
var thisDate = new Date(dateParts[0], dateParts[1], dateParts[2] + i);
console.log(thisDate);
}
結果:
[2011, 4, 1]
Date {Sun May 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Mon May 02 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Tue May 03 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Wed May 04 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Thu May 05 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Fri May 06 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Sat May 07 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Sun May 08 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Mon May 09 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Tue May 10 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Wed May 11 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Thu May 12 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Fri May 13 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
Date {Sat May 14 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
我分裂的日期,我給Date對象的數量是在四月,爲什麼會發生在五月的日期?我對JS有點新,所以可能會有一些明顯的缺失。任何改進的幫助或建議,將不勝感激。
只有一個月零基礎?日間和年份都正常工作。 – Icode4food 2011-03-26 02:30:49
這是真的,但請不要鏈接到w3schools,因爲它是一個可怕的資源。請參閱http://w3fools.com/ – wildcard 2011-03-26 02:33:14
@wildcard - 足夠公平,新的鏈接;) – 2011-03-26 02:35:31