199

I have a current Date object that needs to be incremented by one day using the JavaScript Date object. I have the following code in place:

var ds = stringFormat("{day} {date} {month} {year}", { 
    day: companyname.i18n.translate("day", language)[date.getUTCDay()], 
    date: date.getUTCDate(), 
    month: companyname.i18n.translate("month", language)[date.getUTCMonth()], 
    year: date.getUTCFullYear() 
});

How can I add one day to it?

I've added +1 to getUTCDay() and getUTCDate() but it doesn't display 'Sunday' for day, which I am expecting to happen.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
franticfrantic
  • 2,511
  • 3
  • 17
  • 14
  • 1
    What do you mean by "add +1"? Do you need the next day or something else? – Aleks G Apr 03 '12 at 08:06
  • Does `date: (date.getUTCDate()+1)` not work (works for me)? It's possible that there is a naming conflict with `date` (as Date()-Object _and_ as Object-Key. Have you tried calling the Date()-Object different? – Dominik Schreiber Apr 03 '12 at 08:09
  • currently it show up only as: , no "Sunday 7 Apr 2012" – franticfrantic Apr 03 '12 at 08:09
  • So it's not the `7 Apr` you need (what is 'add next day' to me), it's the `Sunday`. Remember to add the `+1` both in `day:` and `date:` (or, as the current answer mentions, before). – Dominik Schreiber Apr 03 '12 at 08:16

11 Answers11

427

To add one day to a date object:

var date = new Date();

// add a day
date.setDate(date.getDate() + 1);
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 3
    See duplicate question: http://stackoverflow.com/questions/3674539/javascript-date-increment-question. This answer is good, but does not account for DST. There are 2 days of the year that do not have 24 hours. – Jess Jan 09 '14 at 14:48
  • 4
    @Jess—it depends on how you want changes over DST represented. The question asked how to add one to the date, that's exactly what the above answer does. Adding 24 hours doesn't necessarily increment that date, adding one to the local date using [*setDate*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.36) always does. – RobG Jan 12 '14 at 23:39
  • Here is what I mean. Incrementing March 10: `var a = new Date("2013-03-10T00:00:00.000Z");` `a.setDate(a.getDate() + 1);` `a.toISOString();` is `"2013-03-10T23:00:00.000Z"`. This is a subtle case where the above function did not work for me. – Jess Jan 13 '14 at 13:44
  • 1
    Of course not! You start with a UTC time, then essentially convert it to local, add a day, then convert it back to UTC. So one day was added to the **local** date, but only 23 hrs the UTC time. If you add 1 to the UTC date, you'll get back 01:00:00 local time. That behaviour isn't unique to daylight saving, it's a consequence of using three different timezones. – RobG Jan 14 '14 at 00:19
  • 14
    Aha. I didn't realize **[getDate](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate)** was converting from UTC to local time. `a.setUTCDate(a.getUTCDate() + 1);` <-- that works with Zulu dates. – Jess Jan 14 '14 at 01:41
  • 6
    what happens if 31+1 set ? – pronebird Aug 21 '14 at 17:41
  • 1
    @Andy—then it will roll over to the 1st of the next month. – RobG Aug 21 '14 at 22:46
  • @Jess in which browser have you experienced the issue mentioned in your comment on Jan 13 '14 at 13:44? I've just tested in the freshest Chrome, Firefox, Edge, IE11 and all of these gave `2013-03-10T00:00:00.000Z`. Furthermore, the `a.setUTCDate(a.getUTCDate() + 1);` produced the same result as well. – Tamás Bolvári Dec 20 '15 at 15:36
  • @Jess ignore my previous comment, please. I live in Europe, my current time zone is UTC+1, instead of the UTC+5 Indiana zone in which Zulu is. So the conversion is not problematic in my case, but the logic is still wrong, so I'll use the UTC version as you've suggested. RobG's answer adds 24 hours, but I need to increment the day number by 1 (regardless if it's 23 or 24 hours). – Tamás Bolvári Dec 20 '15 at 20:53
  • Hi @TamásBolvári. Maybe you do not have daylight savings in your country or daylight savings begins on a different date. – Jess Dec 21 '15 at 13:04
  • 1
    @Jess I know I'm late to the party, but your comment on using `get`/`setUTCDate()` deserves to be an answer in its own right (and highly upvoted if it were). – TripeHound Mar 30 '21 at 11:24
83

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.
MenyT
  • 1,653
  • 1
  • 8
  • 19
Peter the Russian
  • 1,170
  • 8
  • 9
  • 1
    Adding seconds/milliseconds won't work for daylight savings. – Matt H Nov 02 '15 at 21:47
  • @Matt H where did you get that idea from. The time is set in UTC in milliseconds from 1970/01/01 – Peter the Russian Nov 05 '15 at 15:18
  • 3
    It will be the following day in UTC time, yes, but not necessarily in local time. On Spring Forward day in the US, there are 25 hours in the day. So if you have e.g. 3/31/15 at 00:00 hours, and add 86400000 milliseconds, on Sprint Forward day, the local time would be 3/31/15 at 23:00 hours since there are 25 hours in that day. – Matt H Nov 05 '15 at 18:59
  • 3
    you can see the effect like this: `var dte = Date.parse('2015-11-01'); console.log(dte); dte.setTime(dte.getTime() + 86400000 ); console.log(dte);` – Matt H Nov 05 '15 at 19:10
  • The console will log Sunday midnight, then Sunday 23:00. Using toLocalDateString displays Sunday on both dates (set your timezone to Eastern US to see how that works) – Matt H Nov 05 '15 at 19:11
  • @RobG That wasn't a solution, it was showing the poster how their solution would fail due to daylight savings time. Notice the first part of that comment "you can see the effect like this". – Matt H Dec 21 '15 at 15:22
  • 1
    This is over-engineered. If you're having a problem like this use a library like moment or something to help make things easier. – Joshua Michael Calafell Jan 25 '18 at 18:32
48

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

new Date(Date.now() + ( 3600 * 1000 * 24))
19
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);
serge
  • 13,940
  • 35
  • 121
  • 205
  • 1
    Adding one day of milliseconds fails because not all days are 24 hours long in places that observe daylight saving. E.g. Chile ends daylight saving at midnight on the start of 14 May 2017. Clocks will wind back from midnight to 23:00. So adding 24 hours to 2017-05-13 00:00:00 will produce 2017-05-13 23:00:00, and the date doesn't change (some browsers will do it at exactly midnight, others one millisecond after). :-( – RobG Apr 01 '17 at 22:02
11

simply you can do this

var date = new Date();
date.setDate(date.getDate() + 1);
console.log(date);

now the date will be the date of tomorrow. here you can add or deduct the number of days as you wish.

Saurabh
  • 1,134
  • 5
  • 12
7

Inspired by jpmottin in this question, here's the one line code:

var dateStr = '2019-01-01';
var days = 1;

var result = new Date(new Date(dateStr).setDate(new Date(dateStr).getDate() + days));

document.write('Date: ', result); // Wed Jan 02 2019 09:00:00 GMT+0900 (Japan Standard Time)
document.write('<br />');
document.write('Trimmed Date: ', result.toISOString().substr(0, 10)); // 2019-01-02

Hope this helps

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
1

This is function you can use to add a given day to a current date in javascript.

function addDayToCurrentDate(days){
  let currentDate = new Date()
  return new Date(currentDate.setDate(currentDate.getDate() + days))
}

// current date = Sun Oct 02 2021 13:07:46 GMT+0200 (South Africa Standard Time)
// days = 2

console.log(addDayToCurrentDate(2))
// Mon Oct 04 2021 13:08:18 GMT+0200 (South Africa Standard Time)
ncutixavier
  • 455
  • 5
  • 4
1

// Function gets date and count days to add to passed date
function addDays(dateTime, count_days = 0){
  return new Date(new Date(dateTime).setDate(dateTime.getDate() + count_days));
}

// Create some date
const today = new Date("2022-02-19T00:00:00Z");

// Add some days to date
const tomorrow = addDays(today, 1);

// Result
console.log("Tomorrow => ", new Date(tomorrow).toISOString());
// 2022-02-20T00:00:00.000Z
1

//It is very simple

let d = new Date("22 Apr 2023");

d.setDate(d.getDate() + 1);

console.log(d);

PQ RS
  • 41
  • 3
0

We can get date of the day after today by using timedelta with numOfDays specified as 1 below.

from datetime import date, timedelta

tomorrow = date.today() + timedelta(days=1)
-5
currentDay = '2019-12-06';
currentDay = new Date(currentDay).add(Date.DAY, +1).format('Y-m-d');
bekt
  • 597
  • 4
  • 16