[英]Why JavaScript Date object constructor doesn't work correctly?
看下面的代碼:
var date = new Date();
console.log(date);
// Tue Apr 30 2013 14:24:49 GMT+0430
var date2 = new Date(
date.getFullYear(),
date.getMonth(),
date.getDay(), 0, 0, 0, 0
)
console.log(date2)
// Tue Apr 02 2013 00:00:00 GMT+0430
我只是從今天的日期中提取了某個日期,然后使用該數據創建了另一個日期,結果是另一個日期,而不是今天。 JavaScript的Date對象怎么了?
.getDay()
返回星期幾(0-6),而不是星期幾。 (星期二返回2)
使用getDate()
-它將返回30
getDay()返回星期幾(從0到6),而不是星期幾(1-31)。 正確的方法是getDate():
var date = new Date();
console.log(date);
// Tue Apr 30 2013 14:24:49 GMT+0430
var date2 = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate(), 0, 0, 0, 0
)
console.log(date2)
// Tue Apr 30 2013 00:00:00 GMT+0430
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.