Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add one day to date in javascript

I am sure that a lot of people asked this question but when I checked the answers it seems to me that they are wrong that what I found

var startDate = new Date(Date.parse(startdate)); //The start date is right lets say it is 'Mon Jun 30 2014 00:00:00'  var endDate = new Date(startDate.getDate() + 1); // the enddate in the console will be 'Wed Dec 31 1969 18:00:00' and that's wrong it should be  1 july  

I know that .getDate() return from 1-31 but Does the browser or the javascript increase only the day without updating the month and the year ?

and in this case Should I write an algorithm to handle this ? or there is another way ?

like image 694
kartal Avatar asked Jun 19 '14 17:06

kartal


People also ask

How can I add 1 day to current date?

const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.

How do you increment a date object?

To increment a JavaScript date object by one or more days, you can use the combination of setDate() and getDate() methods that are available for any JavaScript Date object instance. The setDate() method allows you to change the date of the date object by passing an integer representing the day of the month.

How do you add days in date in react JS?

Adding days to current Date const current = new Date(); Now, we can add the required number of days to a current date using the combination of setDate() and getDate() methods.

How do you sum days in JavaScript?

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


1 Answers

Note that Date.getDate only returns the day of the month. You can add a day by calling Date.setDate and appending 1.

// Create new Date instance var date = new Date()  // Add a day date.setDate(date.getDate() + 1) 

JavaScript will automatically update the month and year for you.

EDIT:
Here's a link to a page where you can find all the cool stuff about the built-in Date object, and see what's possible: Date.

like image 159
Aeveus Avatar answered Oct 09 '22 05:10

Aeveus