. Advertisement .
..3..
. Advertisement .
..4..
The programming language used for the web is called JavaScript. Both HTML and CSS can be modified and updated using JavaScript. Javascript can manipulate, calculate, and validate. This blog is about “How To Get the Time Zone Name using JavaScript“. Read on it to open your knowledge.
What Can We Do To Get the Time Zone Name using JavaScript?
Method 1: Use the Intl.DateTimeFormat()
To get the time zone name using JavaScript, you can use the Intl.DateTimeFormat() method, such as Intl.DateTimeFormat (). resolvedOptions(). timeZone The resolvedOptions solution provides a brand-new object on which the timeZone property can be accessed to obtain the time zone’s name.
// "Europe/Sofia"
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
// The Standard Time (en-US locale) of Eastern European
console.log(
new Date()
.toLocaleDateString('en-US', {
day: '2-digit',
timeZoneName: 'long',
})
.slice(4),
);
// Osteuropaeische Normalzeit (de-DE locale)
console.log(
new Date()
.toLocaleDateString('de-DE', {
day: '2-digit',
timeZoneName: 'long',
})
.slice(4),
);
// obtain time zone offset -120 denotes UTC+02 as the time zone offset
const offset = new Date().getTimezoneOffset();
console.log(offset); // -120
Language-specific date and time formatting is possible with the object that the Intl.DateTimeFormat function returns.
Method 2: Use the resolvedOptions()
Another method for you to get the time zone name using JavaScript is using the resolvedOptions(). The timeZone property of the object that the resolved options function returns, which reflects the visitor’s default time zone, is an object.
Method 3: Use the Internationalization API
All of the most recent browsers provide the Internationalization API, which allows for retrieving the user’s timezone.
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Remember that the timeZone field is set to undefine rather than the user’s timezone string on some older browser versions that use the Internationalization API. As far as you can determine, all modern browsers—aside from IE11—will return the user’s timezone as a string at the time of writing (July 2017).
Method 4: Use the toLocaleDateString
Use the toLocaleDateString method if you need the time zone name in a lengthy, localized form, such as Pacific Standard Time. Look at the following example:
// The Standard Time (en-US locale) of Eastern European
console.log(
new Date()
.toLocaleDateString('en-US', {
day: '2-digit',
timeZoneName: 'long',
})
.slice(4),
);
// Osteuropaeische Normalzeit (de-DE locale)
console.log(
new Date()
.toLocaleDateString('de-DE', {
day: '2-digit',
timeZoneName: 'long',
})
.slice(4),
);
We supplied the procedure with the following two parameters:
Locales is a string or collection of strings with BCP 47 language tags. You can choose from various localities, such as en-CA for Canada or es-MX for Mexico.
The options object, where we set the day and timeZoneName attributes, may be found in the MDN docs if you require additional information about this argument. Visit the MDN docs to learn more about the options object. The localized time zone name is represented by the timeZoneName property, which accepts values of long or short.
The output appears as follows when the value of the timeZoneName property is short.
// GMT+2
console.log(
new Date()
.toLocaleDateString('en-US', {
day: '2-digit',
timeZoneName: 'short',
})
.slice(4),
);
// OEZ
console.log(
new Date()
.toLocaleDateString('de-DE', {
day: '2-digit',
timeZoneName: 'short',
})
.slice(4),
);
To remove the date component from the string and simply return the time zone name, we additionally set the day attribute to a two-digit value.
Method 5: Use the getTimezoneOffset()
Use the getTimezoneOffset() function to obtain the time zone offset.
// obtain timezone offset -120 denotes UTC+02 as the time zone offset
const offset = new Date().getTimezoneOffset();
console.log(offset); // -120
The difference between a date (evaluated in UTC) and the exact date (assessed in the visitor’s local time zone), expressed in minutes, is returned by the getTimezoneOffset method.
If you receive a result of the form -120, the time zone offset is UTC+02.
Similarly, UTC+01 is the time zone offset with a value of -60.
Conclusion
If you’re stuck on the error “How To Get the Time Zone Name using JavaScript“, the solution mentioned above is the quickest. People have a vibrant community where everyone is usually ready to help if you still need guidance or have other questions. Finally, we hope you’re having fun with the amazing code alternatives and appreciate your reading time.
Read more
Leave a comment