37

How do I take today's date and add 1 day to it?

If possible, inline please?

Rod
  • 14,529
  • 31
  • 118
  • 230

8 Answers8

61

This will get tomorrow's date:

var a = new Date((new Date()).valueOf() + 1000*3600*24);
cambraca
  • 27,014
  • 16
  • 68
  • 99
  • 1
    Wish I had more upvotes. I believe this is the only one which will still work on the last day of the month. The others will, say, return "32" on the 31st day of the month. – Darren Aug 31 '12 at 17:33
  • 2
    good answer, it is easier just to say: new Date((new Date()).valueOf() + 86400000) or this is broken down into new Date((new Date()).valueOf() + 24*60*60*1000) where you have 24 hours * 60 minutes * 60 seconds * 1000 millisconds – MarzSocks Jul 24 '14 at 09:17
52

You have to use the getDate() and setDate() methods of the Date object which respectively get and set the day value of the date.

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

Check the MDC Date object reference for more information on working with dates

Tom Tu
  • 9,573
  • 35
  • 37
  • 9
    If Daylight Saving Time is followed and if the next day, the time is moved forward/backward, this breaks. So, correct solution would be date.setTime(date.getTime() + 86400000); – venkatagiri Oct 25 '12 at 12:46
  • 3
    I was also having an issue where if the date was the end of the month, it kept wanting to go one day past the day of the month. So December would have 32 days. The solution by Pablo below works. – cbloss793 Dec 19 '14 at 22:16
11

Try this:

//create the date
var myDate = new Date();

//add a day to the date
myDate.setDate(myDate.getDate() + 1);
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Won't this break on the last day of the month? I was using something similar and it would return a day of "32" on the 31st. – Darren Aug 31 '12 at 17:33
5
dt = new Date();
dt.setDate(dt.getDate() + 1);
John Giotta
  • 16,432
  • 7
  • 52
  • 82
1

If by "add 1 day to it" you mean "add 24 hours", that is, add 24*60*60*1000 milliseconds to a JavaScript date object, then the correct solution is:

var d = new Date();
d.setTime(d.getTime() + 86400000);
console.log('24 hours later');
console.log(d);

As @venkatagiri pointed out in an earlier comment, this will in fact add 24 hours to the current JavaScript date object in all scenarios, while d.setDate(d.getDate() + 1) will NOT if a Daylight Savings Time cross-over is involved. See this JSFiddle to see the difference in context of the 2013 start of DST (at March 10, 2013 at 2:00 AM, DST locale time moved forward an hour). setDate() in this scenario only adds 23 hours, while setTime() adds 24.

zachelrath
  • 842
  • 5
  • 11
0
var d = new Date(); 

var curr_date = d.getDate();

var n =curr_date;

jQuery(".class_name:eq(0)").text(n);

var m =[d.getDate()+1];

jQuery(".class_name:eq(1)").text(m);
JF it
  • 2,403
  • 3
  • 20
  • 30
0

Add 30 days and set the date value to datepicker

Example :

$(document).ready(function() {
    var myDate = new Date();

    //add a day to the date
    myDate.setDate(myDate.getDate() + 30);        

    var end_date = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());

    $('#datepicker').datepicker({
        format: 'dd-mm-yyyy',
        orientation: 'bottom'
    });

    $('#datepicker').datepicker('setDate', end_date);
});
AngularJMK
  • 1,178
  • 13
  • 15
0
int days = 1;
var newDate = new Date(Date.now() + days*24*60*60*1000);

From How can I add 1 day to current date? Thanks to Serge

Dami
  • 677
  • 6
  • 12