Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Timezone offset from moment Object?

I have moment Object defined as:

var moment = require('moment');

moment('2015-12-20T12:00:00+02:00');

When I print it, I get:

_d: Sun Dec 20 2015 12:00:00 GMT+0200 (EET)
_f: "YYYY-MM-DDTHH:mm:ssZ"
_i: "2015-12-20T12:00:00+02:00"
_isAMomentObject: true
_isUTC: false
_locale: r
_pf: Object
_tzm: 120

How to fetch by right way _tzm? (suppose its offset in minutes)

Thanks,

like image 902
snaggs Avatar asked Jan 17 '16 12:01

snaggs


People also ask

How do you find UTC offset?

Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How do you use moments with time zones?

For example, if you parse the string '2020/01/02' and then call the moment#tz() method, you're telling moment to parse the string in the locale time, and then convert the date to a given IANA timezone. // '20200101 21:00 PST' moment('2020/01/02', 'YYYY/MM/DD'). tz('America/Los_Angeles'). format('YYYYMMDD HH:mm z');

What is offset in timezone?

What is a "zone offset"? A zone offset is the difference in hours and minutes between a particular time zone and UTC. In ISO 8601, the particular zone offset can be indicated in a date or time value. The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC.


1 Answers

Just access the property like you would in any object

var result = moment('2015-12-20T12:00:00+02:00');


document.body.innerHTML = result._tzm;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>

Another option would be to parse the date and get the zone

moment.parseZone('2015-12-20T12:00:00+02:00').utcOffset(); // 120
// or
moment().utcOffset('2015-12-20T12:00:00+02:00')._offset; // 120
like image 82
adeneo Avatar answered Sep 21 '22 18:09

adeneo