求前三天的日期,为何不对?
var now = new Date() ,beforeDay = new Date(now.getTime() - 24乘60乘60乘1000乘3) ,year = beforeDay.getFullYear() ,month = beforeDay.getMonth() + 1 ,day = beforeDay.getDay(); console.log(year,month,day); if(month < 10) month = ‘0’ + month.toString(); if(day < 10) day = ‘0’ + day.toString(); var before = year.toString() + month + day; console.log('before = '+day,before);
5 回复
getDay获取的是这周的第几天,这个月的第几天应该用getDate
嚓 嚓 ,搞了2个小时,晕倒,忘记了
取日期用getDate()
,而不是getDay()
,那是取星期几。
另外更简单的做法是
var d = new Date();
d.setDate(d.getDate() - 3);
console.log(d);
推荐一个库来解决这个问题: http://momentjs.com/
这个好!