Detect timezone abbreviation using JavaScript
If all else fails, you can simply create your own hashtable with the long names and abbreviations.
How to get timezone name (PDT, EST, etc.) in Javascript?
Using moment.js with the moment-timezone add-on, you can do the following. The results will be consistent regardless of browser or operating system, because the abbreviations are in the data provided by the library.
The example results are from a computer set to the US Pacific time zone:
var zone = moment.tz.guess(); // "America/Los_Angeles"
var abbr = moment.tz(zone).format("z"); // either "PST" or "PDT", depending on when run
DISCLAIMER
Time zone abbreviations are not reliable or consistent. There are many different lists of them, and there are many ambiguities, such as "CST" being for Central Standard Time, Cuba Standard Time and China Standard Time. In general, you should avoid using them, and you should never parse them.
Also, the abbreviations in moment-timezone come from the IANA TZ Database. In recent editions of this data, many "invented" abbreviations have been replaced with numerical offsets instead. For example, if your user's time zone is Asia/Vladivostok
, instead of getting "VLAT"
like you may have in previous versions, you will instead get "+10"
. This is because the abbreviation "VLAT" was originally invented by the tz database to fill the data, and is not in actual use by persons in the area. Keep in mind that abbreviations are always in English also - even in areas where other language abbreviations might be in actual use.
Display local timezone name with moment.js
According to the official moment document, you can use moment-timezone
moment.tz.guess();
For further formatting, refer to this.
Edited :
var zone_name = moment.tz.guess();
var timezone = moment.tz(zone_name).zoneAbbr()
console.log(timezone);
Refer to this working fiddle.
How to convert time zone id to to time zone name in js or moment.js
If you are considering using plain ole JavaScript as an alternative, simply use the Intl.DateTimeFormat() to control any aspect of a date or time display.
Like this:
const time = new Intl.DateTimeFormat("en" , {timeZoneName: "long"});
let date = new Date();
console.log(time.format(date));
Get timezone from users browser using moment(timezone).js
var timedifference = new Date().getTimezoneOffset();
This returns the difference from the clients timezone from UTC time.
You can then play around with it as you like.
Related Topics
Multiply and Sum Input Values Created Dynamically in Jquery
Javascript Function (Input Date Bigger Than Today Date)
How to Toggle Class to a Div Element in Reactjs
Passing Value from Java to JavaScript
Prevent User from Typing in Input At Max Value
React - How to Open Pdf File as a Href Target Blank
Matching a Forward Slash With a Regex
Javascript (+) Sign Concatenates Instead of Giving Sum of Variables
After Clicking a Button of Sweet Alert Call PHP File
How to Parse Excel (Xls) File in Javascript/Html5
How to Avoid Scientific Notation for Large Numbers in JavaScript
React Import: Navigate One Folder Up
Json.Stringify Without Quotes on Properties
Why Is It a Bad Practice to Return Generated HTML Instead of Json or Is It
How to Remember the Value of a Variable After Reloading the Page
React.Js - Input Losing Focus When Rerendering