admin管理员组

文章数量:1384555

I have a JSfiddle which I am trying to add a week onto a date. The date is outputting incorrect date when I try to add six days.

fiddle

code for adding a week

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

I have a JSfiddle which I am trying to add a week onto a date. The date is outputting incorrect date when I try to add six days.

fiddle

code for adding a week

 var endDate =  new Date(date || Date.now()),
            eMonth = '' + (monthNames[endDate.getMonth()]),
            eDay = '' + (endDate.setDate(endDate.getDate() + 6)),
            eYear = endDate.getFullYear();
Share Improve this question edited Feb 12, 2014 at 12:49 Muath 4,42712 gold badges44 silver badges70 bronze badges asked Feb 12, 2014 at 12:34 Jed IJed I 1,0383 gold badges21 silver badges39 bronze badges 1
  • endDate.setDate(endDate.getDate() + 6) this return the endDate (+6 days) in millisecond. :) – Frogmouth Commented Feb 12, 2014 at 12:41
Add a ment  | 

5 Answers 5

Reset to default 4

Try this,

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

Give this a try,


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();
eDay = '' + (endDate.getDate() + 6)

Remove setDate() function and change $("#startDate").text(startDate) to show value in span tag

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

this should do the work

You can also use the get/set Time methods:

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

See the doc about getTime() on MDN

本文标签: jqueryAdding a week to date javascriptStack Overflow