Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add 1 day to current date?

Tags:

javascript

People also ask

How do you add one day to a Date?

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

How can I get current Date in plus one?

To add 1 day to the current date with JavaScript, we can use the setDate method. to get the current day with getDate , then add 1 to it. And then we pass that as the argument of setDate to add 1 to the current date . We can also add 1 day to the current by adding 1 day in milliseconds.

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.


To add one day to a date object:

var date = new Date();

// add a day
date.setDate(date.getDate() + 1);

In my humble opinion the best way is to just add a full day in milliseconds, depending on how you factor your code it can mess up if you are on the last day of the month.

For example Feb 28 or march 31.

Here is an example of how I would do it:

var current = new Date(); //'Mar 11 2015' current.getTime() = 1426060964567
var followingDay = new Date(current.getTime() + 86400000); // + 1 day in ms
followingDay.toLocaleDateString();

Imho this insures accuracy

Here is another example. I do not like that. It can work for you but not as clean as example above.

var today = new Date('12/31/2015');
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate()+1);
tomorrow.toLocaleDateString();

Imho this === 'POOP'

So some of you have had gripes about my millisecond approach because of day light savings time. So I'm going to bash this out. First, Some countries and states do not have Day light savings time. Second Adding exactly 24 hours is a full day. If the date number does not change once a year but then gets fixed 6 months later I don't see a problem there. But for the purpose of being definite and having to deal with allot the evil Date() I have thought this through and now thoroughly hate Date. So this is my new Approach.

var dd = new Date(); // or any date and time you care about 
var dateArray =  dd.toISOString().split('T')[0].split('-').concat( dd.toISOString().split('T')[1].split(':') );
// ["2016", "07", "04", "00", "17", "58.849Z"] at Z 

Now for the fun part!

var date = { 
    day: dateArray[2],
    month: dateArray[1],
    year: dateArray[0],
    hour: dateArray[3],
    minutes: dateArray[4],
    seconds:dateArray[5].split('.')[0],
    milliseconds: dateArray[5].split('.')[1].replace('Z','')
}

Now we have our Official Valid international Date Object clearly written out at Zulu meridian. Now to change the date

dd.setDate(dd.getDate()+1); // this gives you one full calendar date forward
tomorrow.setDate(dd.getTime() + 86400000);// this gives your 24 hours into the future. do what you want with it.

If you want add a day (24 hours) to current datetime you can add milliseconds like this:

new Date(Date.now() + ( 3600 * 1000 * 24))

int days = 1;
var newDate = new Date(Date.now() + days*24*60*60*1000);

CodePen

var days = 2;
var newDate = new Date(Date.now()+days*24*60*60*1000);

document.write('Today: <em>');
document.write(new Date());
document.write('</em><br/> New: <strong>');
document.write(newDate);