久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Javascript -- 檢測用戶的語言環境是否設置為使用

Javascript -- Detect if user#39;s locale are set to use 12-hour or 24-hour time format(Javascript -- 檢測用戶的語言環境是否設置為使用 12 小時或 24 小時時間格式)
本文介紹了Javascript -- 檢測用戶的語言環境是否設置為使用 12 小時或 24 小時時間格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

一種方法是解析 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-USja-JP 是 BCP 語言代碼,傳遞給 Intl.DateTimeFormat 的構造函數以查找日期和時間格式規則.規范使用術語 locale 來指代這條信息,但實際上它是一種語言標識符,雖然它可能包含一個區域(USJP 在提到的代碼中),不需要它們,該區域也不一定指示用戶的語言環境(考慮說西班牙語并在西班牙學習的人[因此將使用語言代碼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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产乱码精品一区二区三区忘忧草 | 免费黄色特级片 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 成人av播放 | 一片毛片 | 一区二区三区四区电影视频在线观看 | 亚洲欧美在线观看 | 中文字幕免费中文 | 99视频在线 | 欧美一级黄色片 | 日本一区二区不卡 | 久久综合九色综合欧美狠狠 | 久久51| 在线视频 亚洲 | 五月婷婷丁香婷婷 | 最近最新中文字幕 | 欧美一级淫片007 | 日本不卡一区 | 日韩成人免费av | 久久黄色| 青春草91| 亚洲精品在线免费播放 | 日本午夜视频 | 色婷婷综合在线观看 | 亚洲 欧美 日韩在线 | 欧美夜夜| 精品成人在线视频 | 国产精品久久久久久久免费大片 | 日韩精品一区二区三区在线播放 | 国产sm主人调教女m视频 | 国产精品99久久久久久宅男 | 成人伊人 | 欧美性生活视频 | 久久亚洲精品国产精品紫薇 | 久久久久久免费精品一区二区三区 | 国产成人影院 | 久久久久国产精品免费免费搜索 | 蜜桃av鲁一鲁一鲁一鲁 | 欧美日韩久久久久 | 国产精品高清一区二区三区 | 中文字幕亚洲视频 |