170

I have the following script which returns the next day:

function today(i)
    {
        var today = new Date();
        var dd = today.getDate()+1;
        var mm = today.getMonth()+1;
        var yyyy = today.getFullYear();

        today = dd+'/'+mm+'/'+yyyy;

        return today;   
    }

By using this:

today.getDate()+1;

I am getting the next day of the month (for example today would get 16).

My problem is that this could be on the last day of the month, and therefore end up returning 32/4/2014

Is there a way I can get the guaranteed correct date for the next day?

Adam Moss
  • 5,582
  • 13
  • 46
  • 64
  • http://stackoverflow.com/questions/1160827/javascript-get-next-day-of-a-string – dikesh Apr 15 '14 at 10:39
  • 1
    Is there any error when going for one liner like `new Date(1000*60*60*24 + +new Date())` ? – Midhun KM Mar 15 '18 at 14:25
  • 1
    @KrIsHnA lovely one-liner. no errors! i'm using it as `new Date(86400000 + +new Date())` to spare processor of cost of multiplication. @ToWhomItMayConcern the plus symbol prefixed here `+new Date()` is Unary operator (Attempts to convert the operand to a number, if it is not already), just in case :) – FAQi Feb 02 '20 at 08:54

3 Answers3

309

You can use:

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

For example, since there are 30 days in April, the following code will output May 1:

var day = new Date('Apr 30, 2000');
console.log(day); // Apr 30 2000

var nextDay = new Date(day);
nextDay.setDate(day.getDate() + 1);
console.log(nextDay); // May 01 2000    

See fiddle.

user1438038
  • 5,821
  • 6
  • 60
  • 94
acarlon
  • 16,764
  • 7
  • 75
  • 94
  • 22
    I don't know if it helps in performance, but it can be simplified by using `tomorrow.setDate(tomorrow.getDate() + 1);` – Gustavo Rodrigues Aug 06 '14 at 14:10
  • 2
    This approach will not update the month. So it will still return April if you ask for the month, in the example – marimaf Nov 24 '17 at 22:40
  • 7
    @marimaf - This approach does update the month. His example was a bit wonky because it depended on it being the same month as when he made this answer (I just updated to fix that) but the approach works. – BryanGrezeszak Feb 16 '18 at 08:33
  • 13
    Here's a oneliner that worked better for my needs: `var tomorrow = new Date(new Date().setDate(new Date().getDate() + 1));` – Björn Andreasson May 22 '19 at 07:43
  • @Björn - the one-liner is great; the only thing is the price of the extra two objects comparing to Gustavo's solution. I couldn't put together any of yours anyway. Cheers guys – Rich Jul 19 '19 at 15:14
  • Does not work for 2016-03-13: `d = new Date("2016-03-13T00:00:00.000Z"), d.setDate(d.getDate()+1), d.toISOString()` – Azmisov Aug 04 '21 at 23:41
  • `var tomorrow = new Date(new Date().setDate(new Date().getDate() + 1)).toLocaleDateString('sv').replaceAll('-', '/');` oneliner with YYYY/MM/DD – 井上智文 Sep 11 '21 at 11:26
64

Copy-pasted from here: Incrementing a date in JavaScript

Three options for you:

Using just JavaScript's Date object (no libraries):

var today = new Date();
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));

One-liner

const tomorrow = new Date(new Date().getTime() + (24 * 60 * 60 * 1000));

Or if you don't mind changing the date in place (rather than creating

a new date):

var dt = new Date();
dt.setTime(dt.getTime() + (24 * 60 * 60 * 1000));

Edit: See also Jigar's answer and David's comment below: var tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1);

Using MomentJS:

var today = moment();
var tomorrow = moment(today).add(1, 'days');

(Beware that add modifies the instance you call it on, rather than returning a new instance, so today.add(1, 'days') would modify today. That's why we start with a cloning op on var tomorrow = ....)

Using DateJS, but it hasn't been updated in a long time:

var today = new Date(); // Or Date.today()
var tomorrow = today.add(1).day();
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
Ruslan F.
  • 5,498
  • 3
  • 23
  • 42
5

Using Date object guarantees that. For eg if you try to create April 31st :

new Date(2014,3,31)        // Thu May 01 2014 00:00:00

Please note that it's zero indexed, so Jan. is 0, Feb. is 1 etc.

mesqueeb
  • 5,277
  • 5
  • 44
  • 77
loxxy
  • 12,990
  • 2
  • 25
  • 56
  • 4
    Note that Number literals start with "0" is octal and has been deprecated. So this code may cause an error in strict mode. And, what you wrote is 03 instead of 04 ... – tsh Jan 20 '15 at 04:42
  • @tsh - It's zero indexed. So 3 is correct for April. – BryanGrezeszak Feb 16 '18 at 08:11