momentJS date string add 5 days

前端 未结 9 1819
死守一世寂寞
死守一世寂寞 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:27

    UPDATED: January 19, 2016

    As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)

    var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
    

    Thanks @Bala for the information.

    UPDATED: March 21, 2014

    This is what you'd have to do to get that format.

    Here's an updated fiddle

    startdate = "20.03.2014";
    var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
    
    var day = new_date.format('DD');
    var month = new_date.format('MM');
    var year = new_date.format('YYYY');
    
    alert(day + '.' + month + '.' + year);
    

    ORIGINAL: March 20, 2014

    You're not telling it how/what unit to add. Use -

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

    You can add days in different formats:

    // Normal adding
    moment().add(7, 'days');
    
    // Short Hand
    moment().add(7, 'd');
    
    // Literal Object    
    moment().add({days:7, months:1});
    

    See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/

    0 讨论(0)
  • 2020-12-23 09:29
    moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:mm'))
    

    has to format and then convert to moment again.

    0 讨论(0)
提交回复
热议问题