添加一周至今的javascript

时间:2014-02-12 12:34:13

标签: javascript jquery date

我有一个JSfiddle,我试图在一个日期添加一周。当我尝试添加六天时,日期输出的日期不正确。

fiddle

添加一周的代码

 var endDate =  new Date(date || Date.now()),
            eMonth = '' + (monthNames[endDate.getMonth()]),
            eDay = '' + (endDate.setDate(endDate.getDate() + 6)),
            eYear = endDate.getFullYear();

5 个答案:

答案 0 :(得分:2)

试试这个,

var endDate = new Date(date || Date.now());
var days = 6;
endDate.setDate(endDate.getDate() + days);

var eMonth = '' + (monthNames[endDate.getMonth()]),
    eDay = '' + endDate.getDate(),
    eYear = endDate.getFullYear();

Working Demo

答案 1 :(得分:0)

eDay = '' + (endDate.getDate() + 6)

删除setDate()功能并更改$("#startDate").text(startDate)以在span标记中显示值

答案 2 :(得分:0)

尝试一下,


var endDate =  new Date(date || Date.now());
endDate.setTime(startDateObj.getTime() + (1000 * 60 * 60 * 24 * 7));
var newDate = endDate.getFullYear()+"-"+(endDate.getMonth() + 1)+"-"+endDate.getDate();

答案 3 :(得分:-1)

您还可以使用get / set Time方法:

var today = new Date();
var plusOneWeek = new Date();
plusOneWeek.setTime( today.getTime()+(7*24*3600*1000) ); //add 7 days

请参阅MDN

上有关getTime()的文档

答案 4 :(得分:-1)

var now = new Date().getTime();
var oneWeek = 6*24*60*60*1000;
var newDate = now+oneWeek;
alert(new Date(newDate));

这应该做的工作