Add a duration to a moment (moment.js)

后端 未结 3 2025
猫巷女王i
猫巷女王i 2020-11-29 22:10

Moment version: 2.0.0

After reading the docs, I thought this would be straight-forward (Chrome console):

var timestring1 = \"2013-05-09T00:00:00Z\";
         


        
3条回答
  •  感情败类
    2020-11-29 22:21

    I am working on an application in which we track live route. Passenger wants to show current position of driver and the expected arrival time to reach at his/her location. So I need to add some duration into current time.

    So I found the below mentioned way to do the same. We can add any duration(hour,minutes and seconds) in our current time by moment:

    var travelTime = moment().add(642, 'seconds').format('hh:mm A');// it will add 642 seconds in the current time and will give time in 03:35 PM format
    
    var travelTime = moment().add(11, 'minutes').format('hh:mm A');// it will add 11 mins in the current time and will give time in 03:35 PM format; can use m or minutes 
    
    var travelTime = moment().add(2, 'hours').format('hh:mm A');// it will add 2 hours in the current time and will give time in 03:35 PM format
    

    It fulfills my requirement. May be it can help you.

提交回复
热议问题