Skip to content Skip to sidebar Skip to footer

Calculate Duration Between Momentjs Timestamps In Utc

I am having a hard time to calculate the duration (difference in time) of two timestamps. There is a timestamp I receive from the server in the following format: start # => '201

Solution 1:

Since your input is UTC you can use moment.utc method.

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

Here a live example:

var start = moment.utc("2017-05-31 06:30:10 UTC", "YYYY-MM-DD HH:mm:ss Z");
var now = moment.utc();
var duration = moment.duration(now.diff(start));
console.log(duration.asHours());
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

In your code sample, you are parsing the input string as local date and then converting it to UTC (with utc() method).

See Local vs UTC vs Offset guide to get more info.

Post a Comment for "Calculate Duration Between Momentjs Timestamps In Utc"