node.jstimezonemomentjsdst

Moment timezone and DST


I am using this code to fetch the GMT difference for a specific time zone:

var TimeZone = 'America/New_York';
console.log('GMT Offset  ' + moment().tz(TimeZone).utcOffset().toString());

Would this code take the DST into account too?


Solution

  • Yes, moment.tz() is daylight savings aware. The docs are explicit about it:

    The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
    ...
    This constructor is DST aware, and will use the correct offset when parsing.

    var TimeZone = 'America/New_York';
    console.log('Winter GMT Offset  ' + moment.tz('2018-12-04', TimeZone).utcOffset()/60);
    console.log('Summer GMT Offset  ' + moment.tz('2018-06-04', TimeZone).utcOffset()/60);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>