Add one day to date in javascript

JavascriptDate

Javascript Problem Overview


I am sure that a lot of people asked this question but when I checked the answers it seems to me that they are wrong that what I found

var startDate = new Date(Date.parse(startdate));
//The start date is right lets say it is 'Mon Jun 30 2014 00:00:00'

var endDate = new Date(startDate.getDate() + 1);
// the enddate in the console will be 'Wed Dec 31 1969 18:00:00' and that's wrong it should be  1 july 

I know that .getDate() return from 1-31 but Does the browser or the javascript increase only the day without updating the month and the year ?

and in this case Should I write an algorithm to handle this ? or there is another way ?

Javascript Solutions


Solution 1 - Javascript

Note that Date.getDate only returns the day of the month. You can add a day by calling Date.setDate and appending 1.

// Create new Date instance
var date = new Date()

// Add a day
date.setDate(date.getDate() + 1)

JavaScript will automatically update the month and year for you.

EDIT:
Here's a link to a page where you can find all the cool stuff about the built-in Date object, and see what's possible: Date.

Solution 2 - Javascript

The Date constructor that takes a single number is expecting the number of milliseconds since December 31st, 1969.

Date.getDate() returns the day index for the current date object. In your example, the day is 30. The final expression is 31, therefore it's returning 31 milliseconds after December 31st, 1969.

A simple solution using your existing approach is to use Date.getTime() instead. Then, add a days worth of milliseconds instead of 1.

For example,

var dateString = 'Mon Jun 30 2014 00:00:00';

var startDate = new Date(dateString);

// seconds * minutes * hours * milliseconds = 1 day 
var day = 60 * 60 * 24 * 1000;

var endDate = new Date(startDate.getTime() + day);

JSFiddle

Please note that this solution doesn't handle edge cases related to daylight savings, leap years, etc. It is always a more cost effective approach to instead, use a mature open source library like moment.js to handle everything.

Solution 3 - Javascript

There is issue of 31st and 28th Feb with getDate() I use this function getTime and 24*60*60*1000 = 86400000

Use this function:


    function incrementDate(dateInput,increment) {
        var dateFormatTotime = new Date(dateInput);
        var increasedDate = new Date(dateFormatTotime.getTime() +(increment *86400000));
        return increasedDate;
    }

Example as below:

var dateWith31 = new Date("2017-08-31");
var dateWith29 = new Date("2016-02-29");

var amountToIncreaseWith = 1; //Edit this number to required input

console.log(incrementDate(dateWith31,amountToIncreaseWith));
console.log(incrementDate(dateWith29,amountToIncreaseWith));

function incrementDate(dateInput,increment) {
        var dateFormatTotime = new Date(dateInput);
        var increasedDate = new Date(dateFormatTotime.getTime() +(increment *86400000));
        return increasedDate;
    }

Solution 4 - Javascript

I think what you are looking for is:

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

Also, you can have a look at Moment.js

> A javascript date library for parsing, validating, manipulating, and formatting dates.

Solution 5 - Javascript

use this i think it is useful for you

var endDate=startDate.setDate(startDate.getDate() + 1);

Solution 6 - Javascript

var datatoday = new Date();
var datatodays = datatoday.setDate(new Date(datatoday).getDate() + 1);
todate = new Date(datatodays);
console.log(todate);

This will help you...

Solution 7 - Javascript

i now it's been long time since this is posted but here my answer

function addDays(date, n)
{
  const oneDayInMs = 86400 * 1000;
  return new Date(Date.parse(date) + (n * oneDayInMs));
}
addDays(new Date(), 1);

Solution 8 - Javascript

Just for the sake of adding functions to the Date prototype:

In a mutable fashion / style:

Date.prototype.addDays = function(n) {
   this.setDate(this.getDate() + n);
};

// Can call it tomorrow if you want
Date.prototype.nextDay = function() {
   this.addDays(1);
};

Date.prototype.addMonths = function(n) {
   this.setMonth(this.getMonth() + n);
};

Date.prototype.addYears = function(n) {
   this.setFullYear(this.getFullYear() + n);
}

// etc...

var currentDate = new Date();
currentDate.nextDay();

Solution 9 - Javascript

add one day in javascript in one line

NB: if you want to add a specific number of days ... just replace 1 with the number of days you want


new Date(new Date().setDate(new Date().getDate() + 1))

console.log(new Date(new Date().setDate(new Date().getDate() + 1)))

Solution 10 - Javascript

If you don't mind using a library, DateJS (https://github.com/abritinthebay/datejs/) would make this fairly easy. You would probably be better off with one of the answers using vanilla JavaScript however, unless you're going to take advantage of some other DateJS features like parsing of unusually-formatted dates.

If you're using DateJS a line like this should do the trick:

Date.parse(startdate).add(1).days();

You could also use MomentJS which has similar features (http://momentjs.com/), however I'm not as familiar with it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkartalView Question on Stackoverflow
Solution 1 - JavascriptAeveusView Answer on Stackoverflow
Solution 2 - JavascriptAustin BrunkhorstView Answer on Stackoverflow
Solution 3 - JavascriptBlack MambaView Answer on Stackoverflow
Solution 4 - JavascriptHJ05View Answer on Stackoverflow
Solution 5 - Javascriptuser3730685View Answer on Stackoverflow
Solution 6 - JavascriptNarendra SagadevanView Answer on Stackoverflow
Solution 7 - Javascriptabel-makView Answer on Stackoverflow
Solution 8 - JavascriptNatalie PerretView Answer on Stackoverflow
Solution 9 - JavascriptMg CodeurView Answer on Stackoverflow
Solution 10 - JavascriptEctropyView Answer on Stackoverflow