Detect timezone abbreviation using JavaScript

后端 未结 16 2075
清酒与你
清酒与你 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

    I was able to achieve this with only moment.

    moment.tz(moment.tz.guess()).zoneAbbr() //IST
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-30 02:38

    For a crossbrowser support I recommend using YUI 3 Library:

    Y.DataType.Date.format(new Date(), {format:"%Z"});
    

    It supports strftime identifiers.

    For more information: http://yuilibrary.com/yui/docs/datatype/#dates

    0 讨论(0)
  • 2020-11-30 02:42

    Here is a JavaScript self-updating, 12-hour format date/time display that doesn't quite answer the question, however it may help others as it is related and builds on Stephen DuMont's solution and MDN link. W3 Schools had a very helpful tutorial, and real-time updates do not require page refresh.

    Tests with the latest versions of desktop FireFox, Chrome, Opera, and Internet Explorer 11 all work. The "2-digits" hour only appears to prefix a zero for single values in IE, however the minutes return a 2-digit value reliably for all browsers. Tests with discontinued Windows Safari work although 12-hour format is ignored and seconds are not hidden.

    The function includes the local timezone, as well adjustable options for fall-back languages, day and date display, and 12/24 hour format. Date and time were split to add the separating 'at' string. Setting only 'toLocaleTimeString' with select options will also return both date and time. The MDN pages can be referenced for options and values.

    <!--
    function dateTimeClock() {
      var date = new Date();
      document.getElementById('timedate').innerHTML = date.toLocaleDateString(['en-us', 'en-GB'], {
          weekday: 'long',
          month: 'long',
          day: '2-digit',
          year: 'numeric'
        }) + ' at ' +
        date.toLocaleTimeString(['en-us', 'en-GB'], {
          hour12: 'true',
          hour: '2-digit',
          minute: '2-digit',
          timeZoneName: 'short'
        });
      var t = setTimeout(dateTimeClock, 500);
    }
    
    function start() {
      dateTimeClock();
    }
    window.onload = start;
    //-->
    <div id="timedate"></div>

    0 讨论(0)
  • 2020-11-30 02:44
    import { tz } from 'moment-timezone';
    import * as moment from 'moment';
    
    const guess = tz.guess(true);    // "Asia/Calcutta"
    const zone = tz.zone(guess);     // return Zone object 
    zone.abbr(new Date().getTime())  // "IST" 
    // once u pass timestamp it'll return timezone abbrevation.
    
    0 讨论(0)
  • 2020-11-30 02:45

    The Date object doesn't have a method for getting the timezone abbreviation, but it is implicit at the end of the result of toString. For example,

    var rightNow = new Date();
    alert(rightNow);
    

    ...will return something like Wed Mar 30 2011 17:29:16 GMT-0300 (ART). The timezone abbreviation can be isolated between parentheses:

    var rightNow = new Date();
    alert(String(String(rightNow).split("(")[1]).split(")")[0]);
    

    The output will be the timezone abbreviation, like ART.

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