Add one day to date in javascript

后端 未结 8 1295
再見小時候
再見小時候 2020-12-14 06:00

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 Da         


        
相关标签:
8条回答
  • 2020-12-14 06:08

    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.

    0 讨论(0)
  • 2020-12-14 06:14

    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.

    0 讨论(0)
  • 2020-12-14 06:16

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

    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;
        }

    0 讨论(0)
  • 2020-12-14 06:19

    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.

    0 讨论(0)
  • 2020-12-14 06:19
    var datatoday = new Date();
    var datatodays = datatoday.setDate(new Date(datatoday).getDate() + 1);
    todate = new Date(datatodays);
    console.log(todate);
    

    This will help you...

    0 讨论(0)
  • 2020-12-14 06:23

    use this i think it is useful for you

    var endDate=startDate.setDate(startDate.getDate() + 1);
    
    0 讨论(0)
提交回复
热议问题