How can I get the timezone name in JavaScript?

后端 未结 8 1623
时光取名叫无心
时光取名叫无心 2020-12-12 15:37

I know how to get the timezone offset, but what I need is the ability to detect something like \"America/New York.\" Is that even possible from JavaScript or is that somethi

8条回答
  •  一向
    一向 (楼主)
    2020-12-12 16:03

    In javascript , the Date.getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.

    var x = new Date();
    var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
    

    Moment'timezone will be a useful tool. http://momentjs.com/timezone/

    Convert Dates Between Timezones

    var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
    var losAngeles = newYork.clone().tz("America/Los_Angeles");
    var london     = newYork.clone().tz("Europe/London");
    
    newYork.format();    // 2014-06-01T12:00:00-04:00
    losAngeles.format(); // 2014-06-01T09:00:00-07:00
    london.format();     // 2014-06-01T17:00:00+01:00
    

提交回复
热议问题