如果我有一个日期是2011-01-02,并且从该日期减去7天,它应该给我2010-12-26,但是却给了我2011-01-26吗?

请参阅下面的JS以通过链接进行验证:

var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate = newdate.setDate(newdate.getDate() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);

http://jsbin.com/upeyu/6

最佳答案

我认为您打算这样做:(完美运行)

var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);

jsFiddle example

10-08 14:32