[英]How can I get the timezone name in JavaScript?
我知道如何獲得時區偏移量,但我需要的是能夠檢測到“美國/紐約”之類的東西。 這甚至可能來自 JavaScript 還是我將不得不根據偏移量推測的東西?
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
請記住,在一些支持國際化 API 的舊瀏覽器版本上, timeZone
屬性設置為undefined
而不是用戶的時區字符串。 據我所知,在撰寫本文時(2017 年 7 月), 除 IE11 之外的所有當前瀏覽器都將用戶時區作為字符串返回。
大多數贊成的答案可能是獲取時區的最佳方法,但是, Intl.DateTimeFormat().resolvedOptions().timeZone
根據定義返回 IANA 時區名稱,該名稱是英文的。
如果您想要當前用戶語言的時區名稱,您可以從Date
的字符串表示中解析它,如下所示:
function getTimezoneName() { const today = new Date(); const short = today.toLocaleDateString(undefined); const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' }); // Trying to remove date from the string in a locale-agnostic way const shortIndex = full.indexOf(short); if (shortIndex >= 0) { const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length); // by this time `trimmed` should be the timezone's name with some punctuation - // trim it from both sides return trimmed.replace(/^[\\s,.\\-:;]+|[\\s,.\\-:;]+$/g, ''); } else { // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name return full; } } console.log(getTimezoneName());
在 Chrome 和 Firefox 中測試。
當然,這在某些環境中不會按預期工作。 例如,node.js 返回 GMT 偏移量(例如GMT+07:00
)而不是名稱。 但我認為它仍然可以作為后備閱讀。
PS 在 IE11 中不起作用,就像Intl...
解決方案一樣。
您可以使用此腳本。 http://pelepim.bitbucket.org/jstz/
在此處分叉或克隆存儲庫。 https://bitbucket.org/pelepim/jstimezonedetect
包含腳本后,您可以在 - jstz.olson.timezones
變量中獲取時區列表。
以下代碼用於確定客戶端瀏覽器的時區。
var tz = jstz.determine();
tz.name();
享受jstz!
按名稱檢索時區(即“美國/紐約”)
moment.tz.guess();
當前用戶語言的結果的簡短可能性:
console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));
您可以使用此處的映射表簡單地編寫自己的代碼: http : //www.timeanddate.com/time/zones/
或者,使用時刻時區庫: http : //momentjs.com/timezone/docs/
見zone.name; // America/Los_Angeles
zone.name; // America/Los_Angeles
或者,這個庫: https : //github.com/Canop/tzdetect.js
要檢測諸如“America/New York.”之類的內容,您可以使用Luxon 庫中的new LocalZone()
。
import { LocalZone } from 'luxon';
const zoneName = new LocalZone().name;
試試這個代碼參考here
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone = tz.name(); //'Asia/Kolhata' for Indian Time.
alert(timezone);
});
</script>
這將獲取舊版 javascript 中的時區代碼(例如GMT
)(我使用的是帶有舊引擎的谷歌應用程序腳本):
function getTimezoneName() {
return new Date().toString().get(/\((.+)\)/);
}
我只是把它放在這里以防有人需要它。
在 javascript 中, Date.getTimezoneOffset() 方法返回當前語言環境相對於 UTC 的時區偏移量(以分鍾為單位)。
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
Moment'timezone 將是一個有用的工具。 http://momentjs.com/timezone/
在時區之間轉換日期
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london = newYork.clone().tz("Europe/London");
newYork.format(); // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format(); // 2014-06-01T17:00:00+01:00
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.