Add hours minutes with the format hh:mm using moment

前端 未结 5 422
自闭症患者
自闭症患者 2021-01-21 00:55

I am using moment library and from front end I am getting time in HH:mm I need to add hours and minutes to current time using moment.

Suppose I am getting <

5条回答
  •  盖世英雄少女心
    2021-01-21 01:37

    EDIT: @VincenzoC above provided what I personally think is the best practice. Check this answer only as an alternative to his.

    Just do:

    moment().add({
       hours: 5,
       minutes: 30
    });
    

    As suggested in the official documentation: http://momentjs.com/docs/#/manipulating/add/

    Either use the object literal as above, or use chaining.

    I you can't get rid of that string, just acquire hours and minutes by splitting:

    let [hours, minutes] = '05:30'.split(':').map(Number);
    console.log(hours);
    console.log(minutes);

提交回复
热议问题