Detect timezone abbreviation using JavaScript

后端 未结 16 2130
清酒与你
清酒与你 2020-11-30 02:17

I need a way to detect the timezone of a given date object. I do NOT want the offset, nor do I want the full timezone name. I need to get the timezone abbreviation.

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 02:37

    try {
        result = /.*\s(.+)/.exec(date.toLocaleDateString(navigator.language, {timeZoneName:'short' }))[1];
    } catch(e) {
        result = (new Date()).toTimeString().match(new RegExp("[A-Z](?!.*[\(])","g")).join('');
    }
    
    console.log(result);
    

提交回复
热议问题