时刻时区和 DST

Moment timezone and DST

我正在使用此代码获取特定时区的 GMT 时差:

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

此代码也会考虑夏令时吗?

是的,moment.tz() 支持夏令时。 docs 对此很明确:

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>