Get timezone difference in hours with moment

后端 未结 1 1962
悲&欢浪女
悲&欢浪女 2020-12-11 02:24

I would like to get the timezone difference between New York and Hong Kong with Node.js moment module. I have done some preliminary work.

var NewYork_time_hr         


        
相关标签:
1条回答
  • 2020-12-11 02:28

    Not sure about "simpler", but more correct (since not all timezones are a full hour from each other):

    // get the current time so we know which offset to take (DST is such bullkitten)
    var now = moment.utc();
    // get the zone offsets for this time, in minutes
    var NewYork_tz_offset = moment.tz.zone("America/New_York").offset(now); 
    var HongKong_tz_offset = moment.tz.zone("Asia/Hong_Kong").offset(now);
    // calculate the difference in hours
    console.log((NewYork_tz_offset - HongKong_tz_offset) / 60);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.1/moment-timezone-with-data-2010-2020.min.js"></script>

    0 讨论(0)
提交回复
热议问题