momentJS date string add 5 days

前端 未结 9 1818
死守一世寂寞
死守一世寂寞 2020-12-23 09:03

i have a start date string \"20.03.2014\" and i want to add 5 days to this with moment.js but i don\'t get the new date \"25.03.2014\" in the alert window.

here my j

相关标签:
9条回答
  • 2020-12-23 09:08

    updated:

    startdate = "20.03.2014";
    var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
    
    alert(new_date)
    
    0 讨论(0)
  • 2020-12-23 09:09
    1. add https://momentjs.com/downloads/moment-with-locales.js to your html page
    2. var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date then
    3. var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..

    point 2 and 3 are using in your jquery code...

    0 讨论(0)
  • 2020-12-23 09:11
    var end_date = moment(start_date).clone().add(5, 'days');
    
    0 讨论(0)
  • 2020-12-23 09:14

    The function add() returns the old date, but changes the original date :)

    startdate = "20.03.2014";
    var new_date = moment(startdate, "DD.MM.YYYY");
    new_date.add(5, 'days');
    alert(new_date);
    
    0 讨论(0)
  • 2020-12-23 09:20

    You can reduce what they said in a few lines of code:

    var nowPlusOneDay = moment().add('days', 1);
    var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
    
    alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
    alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
    
    0 讨论(0)
  • 2020-12-23 09:25

    To get an actual working example going that returns what one would expect:

    var startdate = "20.03.2014";
    var new_date = moment(startdate, "DD.MM.YYYY");
    var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
    window.console.log(thing)
    
    0 讨论(0)
提交回复
热议问题