Add hours minutes with the format hh:mm using moment

前端 未结 5 375
自闭症患者
自闭症患者 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:31

    You can create a duration for '05:30' using moment.duration(String) and the use add(Duration).

    As moment duration docs states, '05:30' is a valid input for moment.duration(String):

    As of 2.1.0, moment supports parsing ASP.NET style time spans. The following formats are supported.

    The format is an hour, minute, second string separated by colons like 23:59:59. The number of days can be prefixed with a dot separator like so 7.23:59:59. Partial seconds are supported as well 23:59:59.999.

    Here a live sample:

    const result = moment().add( moment.duration('05:30') );
    console.log( moment().format() );
    console.log( result.format() );

提交回复
热议问题