Detect timezone abbreviation using JavaScript

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

    Using contents from new Date().toString()

    const timeZoneAbbreviated = () => {
      const { 1: tz } = new Date().toString().match(/\((.+)\)/);
    
      // In Chrome browser, new Date().toString() is
      // "Thu Aug 06 2020 16:21:38 GMT+0530 (India Standard Time)"
    
      // In Safari browser, new Date().toString() is
      // "Thu Aug 06 2020 16:24:03 GMT+0530 (IST)"
    
      if (tz.includes(" ")) {
        return tz
          .split(" ")
          .map(([first]) => first)
          .join("");
      } else {
        return tz;
      }
    };
    
    console.log("Time Zone:", timeZoneAbbreviated());
    // IST
    // PDT
    // CEST

    0 讨论(0)
  • 2020-11-30 03:00

    This is a tricky subject. From what I gather the timezone is not embedded as part of the Date object when it's created. You also cannot set the timezone for a Date object. However, there is a timezone offset you can get from a Date object which is determined by the user's host system (timezone) settings. Think of timezone as a way to determine the offset from UTC.

    To make life easier, I highly recommend moment and moment-timezone for handling these things. Moment creates a wrapper object for Date with a nice API for all kinds of things.

    If an existing date object is supplied to you through some parameter or something, you can pass it the constructor when creating a the moment object and you're ready to roll. At this point you can use moment-timezone to guess the user's timezone name, and then use moment-timezone formatting to get the abbreviation for the timezone. I would venture to say that most users have their timezone set automatically but you shouldn't rely on this for 100% accuracy. If needed you can also set the timezone you want to use manually before pulling the format you need.

    In general when working with date and time it's best to store UTC in your database and then use moment js to format the time for the user's timezone when displaying it. There may be cases where you need to be sure the timezone is correct. For example if you are allowing a user to schedule something for a specific date and time. You would need to make absolutely sure that with a west coast user that you set the timezone to PDT/PST before converting to UTC for storage in your database.

    Regarding the timezone abbreviation...
    Here is a basic example to get the timezone abbreviation using moment and moment-timezone.

    // if all you need is the user's timezone abbreviation you don't even need a date object.
    const usersTimezoneName = moment.tz.guess()
    const timezoneAbbr = moment().tz(usersTimezoneName).format('z')
    console.log(timezoneAbbr) // PST (depending on where the user is)
    
    // to manually set the timezone
    const newYorkAbbr = moment(dateObj).tz('America/New_York').format('z')
    console.log(newYorkAbbr) // EST
    

    For displaying a specific date object with offsets for a specific timezone you can do this.

    const xmas = new Date('December 25, 2017 16:20:00')
    const losAngelesXmas = moment(xmas).tz('America/Los_Angeles')
    console.log(losAngelesXmas.format("ffffdd, MMMM Do YYYY, h:mm:ss a")) // Monday, December 25th 2017, 4:20:00 pm
    
    0 讨论(0)
  • 2020-11-30 03:01

    If all else fails, you can simply create your own hashtable with the long names and abbreviations.

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

    A native solution:

    var zone = new Date().toLocaleTimeString('en-us',{timeZoneName:'short'}).split(' ')[2]
    console.log(zone)

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

    You can pass undefined instead of en-us to default to the browser's current locale.

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