admin管理员组文章数量:1429735
I already tried the codes below, but they don't work because they include Date and\or Time. I need to get only the timezone name. (OBS: timezone name, not IANA Time Zone)
Is it possible to force toLocaleString() to show timeZoneName "short" and "long" at same time?
Example: 5/2/2021, 02:34:28 GMT+12:45 Chatham Standard Time
Get_TimeZone_Name(Functions) for Modern Browsers (2017-2018+) and for IE11!
<!DOCTYPE html>
<div id="Output"></div>
<script>
Date_Object = new Date();
document.getElementById("Output").innerText =
Date_Object.toLocaleString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"short"}) + "\n" +
"\n" +
Date_Object.toLocaleString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("en-US", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("pt-BR", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ja-JP", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ar-SA", {timeZoneName:"long"}) + "\n" +
"";
</script>
I already tried the codes below, but they don't work because they include Date and\or Time. I need to get only the timezone name. (OBS: timezone name, not IANA Time Zone)
Is it possible to force toLocaleString() to show timeZoneName "short" and "long" at same time?
Example: 5/2/2021, 02:34:28 GMT+12:45 Chatham Standard Time
Get_TimeZone_Name(Functions) for Modern Browsers (2017-2018+) and for IE11!
<!DOCTYPE html>
<div id="Output"></div>
<script>
Date_Object = new Date();
document.getElementById("Output").innerText =
Date_Object.toLocaleString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"short"}) + "\n" +
"\n" +
Date_Object.toLocaleString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("en-US", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("pt-BR", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ja-JP", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ar-SA", {timeZoneName:"long"}) + "\n" +
"";
</script>
Share
Improve this question
edited May 3, 2021 at 23:00
user
asked Apr 30, 2021 at 23:48
useruser
435 bronze badges
2 Answers
Reset to default 6there you go, you just need 4 step:
- get simple date string
- get long time zone name
- get short time zone name
- append them together
see the code below
let date = new Date();
let dateString = date.toLocaleString();
let shortTimeZone = getTimeZoneName(date,[],'short');
let longTimeZone = getTimeZoneName(date,[],'long');
console.log(`${dateString} ${shortTimeZone} ${longTimeZone}`)
/**
* date: Date = date object
* locales: string | [] = 'en-us' | []
* type: string = 'short' | 'long'
**/
function getTimeZoneName(date,locales,type) {
return new Intl.DateTimeFormat(locales, { timeZoneName: type })
.formatToParts(date)
.find(part => part.type == "timeZoneName")
.value
}
get this idea from ThirstyMonkey
you can find more usage of Intl.DateTimeFormat
on MDN
there is no way to force .toLocaleString
to show short timezone and long timezone at the same time
how about using this solution
Date_Object = new Date();
myTimezoneFormat(Date_Object)
here is the myTimezoneFormat
function
function myTimezoneFormat(date, locale="us") {
return date.toLocaleString(locale, {timeZoneName:"short"}).split(" ").slice(2).join(" ")+" "+ date.toLocaleString(locale, {timeZoneName:"long"}).split(" ").slice(2).join(" ");
}
本文标签: dateHow to get only the timeZone name in JavaScriptStack Overflow
版权声明:本文标题:date - How to get only the timeZone name in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745496413a2660825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论