Skip to content Skip to sidebar Skip to footer

Momentjs And Timezone/utc Calculations

I have a JavaScript date object which has come from an API; but when I use Moment to do some date manipulation, the timezone component is messed up and the resulting date now uses

Solution 1:

By definition, Date objects are just in UTC:

Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC.

The browser usually will transform this to local time, though.

I am unsure what you are trying to accomplish. However, in general, for printing back the date in a different timezone, string would be the way. As for timezone manipulation, Moment Timezone might be your place to go.

That being said, if your use case is simple, you can try directly parsing the string date moment and using parseZone. It will allow you to capture the exact date and get timezone information as well.

Just a small snippet showing this.

const date = '2018-09-30T00:00:00+10:00';

const parseZone = moment.parseZone(date);
console.log('Original time: ', parseZone.format());

const offsetTimezone = parseZone.format('Z');
console.log('Timezone', offsetTimezone);

const utcOffset = parseZone.utcOffset();
console.log('Offset from UTC in minutes', utcOffset);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Post a Comment for "Momentjs And Timezone/utc Calculations"