📅  最后修改于: 2023-12-03 15:30:54.020000             🧑  作者: Mango
GetNameOfZone is a Javascript function that returns the name of the timezone of a given date.
GetNameOfZone(date);
date
: A valid Javascript Date object.
The name of the timezone of the specified date.
var date = new Date();
var zoneName = GetNameOfZone(date);
console.log(zoneName);
The output will vary depending on the timezone, but it could be something like:
Pacific Standard Time
function GetNameOfZone(date) {
var tz = date.toLocaleTimeString('en-us',{timeZoneName:'long'}).split(' ')[2];
return tz;
}
The GetNameOfZone
function receives a Date
object as a parameter. We then use the toLocaleTimeString
method to obtain a formatted string of the specified date, but we pass the timeZoneName
option as 'long'
to obtain the full timezone name.
We then split the string into an array of words and get the third element, which is where the timezone name is. Finally, we return the name of the timezone.
GetNameOfZone is a simple yet useful function that can help you obtain the timezone name of a given date in Javascript.