Calculate the UTC offset given a TimeZone string in JavaScript

前端 未结 5 2067
感情败类
感情败类 2020-12-06 11:00

Using the standard JS library (ECMA5), without using momentjs or external libs, how do you calculate the UTC offset given a TimeZone string such as \"Europe/Rome\"

5条回答
  •  执笔经年
    2020-12-06 11:42

    For others who may land on this question, there is a method available on the standard Date function in JavaScript to get the offset from UTC called getTimezoneOffset.

    Usage is simply (for example): new Date().getTimezoneOffset(). This returns the number of minutes the current timezone is from UTC. NOTE: It returns a negative number if the current timezone is ahead of UTC. For example, if the user's timezone is UTC+1, -60 is returned.

提交回复
热议问题