Search code examples
javascriptdate

JavaScript, get date of the next day


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?


Solution

  • 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.