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 ?
const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.
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.
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.
var today = new Date(); var tomorrow = new Date(); tomorrow. setDate(today. getDate()+1);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With