Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add day(s) to a Date object [duplicate]

Tags:

javascript

Possible Duplicate:
How to add number of days to today's date?

I'm confused, I found so many different approaches, and which of them is actually correct?

So what is the correct way to add day(s) to a given Date?

like image 629
iLemming Avatar asked Aug 05 '11 22:08

iLemming


People also ask

How do you get day from date object?

prototype. getDay() The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

How can you tell if two date objects are on the same day?

To check if two dates are the same day, call the toDateString() method on both Date() objects and compare the results. If the output from calling the method is the same, the dates are the same day.

How do you add days to a new date?

var today = new Date(); var tomorrow = new Date(); tomorrow. setDate(today. getDate()+1);


2 Answers

date.setTime( date.getTime() + days * 86400000 ); 
like image 104
nobody Avatar answered Sep 27 '22 00:09

nobody


Note : Use it if calculating / adding days from current date.

Be aware: this answer has issues (see comments)

var myDate = new Date(); myDate.setDate(myDate.getDate() + AddDaysHere); 

It should be like

var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));

like image 23
user710502 Avatar answered Sep 25 '22 00:09

user710502