問題描述
一種方法是解析 new Date().toLocaleString()
.但這在 chromium/webkit 中不起作用,因為它返回的字符串不依賴于用戶的語言環境(請參閱 http://code.google.com/p/chromium/issues/detail?id=3607)
One way to do that is to parse new Date().toLocaleString()
.
But this doesn't work in chromium/webkit since the string it returns isn't dependent of the user's locale (see bug report at http://code.google.com/p/chromium/issues/detail?id=3607)
我強調我正在尋找一種僅適用于客戶端且適用于 chromium 的解決方案.
I emphasize that I'm looking for a solution that is client side only and that works in chromium.
推薦答案
距離上次回答這個問題已經有幾年了,并且已經引入了一些技術來解決這個問題.一種這樣的技術是 Intl.DateTimeFormat
,它提供了有關各種語言環境的日期格式的大量信息.
It's been a few years since this was last answered and a few technologies have been introduced to solve the issue. One such technology is Intl.DateTimeFormat
, which provides a wealth of information about date formats for various locales.
console.log(new Intl.DateTimeFormat().resolvedOptions());
console.log(new Intl.DateTimeFormat().resolvedOptions().hour12);
.as-console-wrapper { max-height: 100% !important; }
但是,大多數語言環境沒有為 hour12
選項定義默認值.所以,如果這返回 undefined
,我會查看 formatToParts
函數.
However, most locales don't define a default for the hour12
option. So, if this returns undefined
, I'd look at the formatToParts
function.
const hourParts = new Intl.DateTimeFormat(undefined, { hour: 'numeric' }).formatToParts(new Date(2020, 0, 1, 13));
console.log(hourParts);
.as-console-wrapper { max-height: 100% !important; }
輸出應該如下所示(對于您的瀏覽器的當前語言;在我的例子中,en-US"):
The output from that should look like (for your browser's current language; in my case, "en-US"):
[
{
"type": "hour",
"value": "1"
},
{
"type": "literal",
"value": " "
},
{
"type": "dayPeriod",
"value": "PM"
}
]
獲取 type
等于 小時"
部分的 value
的長度將告訴您它是否被格式化為 12或二十四小時制.
Getting the length of the value
of the part with type
equal to "hour"
will tell you whether it was formatted with twelve or twenty-four hour time.
例如,我碰巧知道在日本,他們使用 24 小時制,所以我可以檢查一下:
For instance, I happen to know that in Japan, they use twenty four hour time, so I can check that:
const hourParts = new Intl.DateTimeFormat('ja-JP', {
hour: 'numeric'
}).formatToParts(new Date(2020, 0, 1, 13));
console.log(hourParts.find(part => part.type === 'hour').value.length);
.as-console-wrapper { max-height: 100% !important; }
而且我知道美國默認為十二小時時間:
And I know the the US defaults to twelve hour time:
const hourParts = new Intl.DateTimeFormat('en-US', {
hour: 'numeric'
}).formatToParts(new Date(2020, 0, 1, 13));
console.log(hourParts.find(part => part.type === 'hour').value.length);
.as-console-wrapper { max-height: 100% !important; }
將它包裝在一個函數中很容易:
It would be easy enough to wrap this in a function:
function localeUses24HourTime(langCode) {
return new Intl.DateTimeFormat(langCode, {
hour: 'numeric'
}).formatToParts(new Date(2020, 0, 1, 13)).find(part => part.type === 'hour').value.length === 2;
}
console.log(localeUses24HourTime()); // undefined means current user's language
console.log(localeUses24HourTime('en-US')); // a specific language known to be false
console.log(localeUses24HourTime('ja-JP')); // a specific language known to be true
.as-console-wrapper { max-height: 100% !important; }
您可能會發現這或多或少比解析 toLocaleString()
的輸出復雜.
You may find this more or less complicated than parsing the output of toLocaleString()
.
注意:我不再使用語言環境"一詞,而是使用語言環境"一詞.到語言"在我的回答中途.這是由于規范的編寫方式和信息的指定方式.en-US
和 ja-JP
是 BCP 語言代碼,傳遞給 Intl.DateTimeFormat
的構造函數以查找日期和時間格式規則.規范使用術語 locale
來指代這條信息,但實際上它是一種語言標識符,雖然它可能包含一個區域(US
和 JP
在提到的代碼中),不需要它們,該區域也不一定指示用戶的語言環境(考慮說西班牙語并在西班牙學習的人[因此將使用語言代碼es-ES"],但生活在美國,那里的日期格式與西班牙不同).
Note: I switched from using the term "locale" to "language" midway through my answer. This is due to how the specification is written and how information is specified. en-US
and ja-JP
are BCP language codes, passed to the constructor of Intl.DateTimeFormat
to look up date and time formatting rules. The specification uses the term locale
to refer to this piece of information but indeed it is a language identifier, which, while it may contain a region (the US
and JP
in the mentioned codes), does not require them, nor does that region necessarily indicate the user's locale (consider a person who speaks Spanish and learned it in Spain [and therefore would use the language code 'es-ES'], but lives in the United States, where the dates are formatted differently than in Spain).
這篇關于Javascript -- 檢測用戶的語言環境是否設置為使用 12 小時或 24 小時時間格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!