Detect timezone abbreviation using JavaScript

Viewed 88207

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.

For example, GMT, UTC, PST, MST, CST, EST, etc...

Is this possible? The closest I've gotten is parsing the result of date.toString(), but even that won't give me an abbreviation. It gives me the timezone's long name.

20 Answers

Update for 2021

moment.js is now deprecated and they suggest using Luxon in new projects instead.

You can get a timezone abbreviation with Luxon like:

import { DateTime } from 'luxon'
DateTime.local().toFormat('ZZZZ') // => for ex: "PDT"

Note: The output format depends on the set locale. For more information see this answer.

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

You can use the formatToParts method of Intl.DateTimeFormat to get the short time zone abbreviation. In certain parts of the world this may return strings like "GMT+8", rather than the abbreviation of an actual time zone name.

let timeZone = new Intl.DateTimeFormat('en-us', { timeZoneName: 'short' })
  .formatToParts(new Date())
  .find(part => part.type == "timeZoneName")
  .value
console.log(timeZone)

You can use the Native Javascript date object. Just hardcode the long timezone name 'America/Los_Angeles.

var usertimezone = 'America/Los_Angeles';
var usertimezoneabbreviation = new Date().toLocaleTimeString('en-us',{timeZone: usertimezone, timeZoneName:'short'}).split(' ')[2];

console.log(usertimezoneabbreviation); //PDT

Try this function

function getTimezoneAbbreviation(date) {
  // Convert the date into human readable
  // An example: Fri Jul 09 2021 13:07:25 GMT+0200 (Central European Summer Time)
  // As you can see there is the timezone

  date = date.toString();

  // We get the timezone matching everything the is inside the brackets/parentheses
  var timezone = date.match(/\(.+\)/g)[0];

  // We remove the brackets/parentheses
  timezone = timezone.replace("(", "").replace(")", "");

  // A new variable for the abbreviation
  var abbreviation = "";

  // We run a forEach dividing the timezone in words with split
  timezone.split(" ").forEach((word) => {

      // We insert the first letter of every word in the abbreviation variable
      abbreviation += word.split("")[0];

  });

  // And finally we return the abbreviation
  return abbreviation;
}

Try Google's Closure Class goog.i18n.DateTimeSymbols and their locale related classes.

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.
Related