Get the time difference between two datetimes

前端 未结 19 1968
花落未央
花落未央 2020-11-22 05:25

I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I\'m having a hard time trying to do something that seems simple: geting the differ

19条回答
  •  情深已故
    2020-11-22 06:02

    When you call diff, moment.js calculates the difference in milliseconds. If the milliseconds is passed to duration, it is used to calculate duration which is correct. However. when you pass the same milliseconds to the moment(), it calculates the date that is milliseconds from(after) epoch/unix time that is January 1, 1970 (midnight UTC/GMT). That is why you get 1969 as the year together with wrong hour.

    duration.get("hours") +":"+ duration.get("minutes") +":"+ duration.get("seconds")
    

    So, I think this is how you should do it since moment.js does not offer format function for duration. Or you can write a simple wrapper to make it easier/prettier.

提交回复
热议问题