久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 亚洲久草视频 | www.婷婷| 在线免费看91 | 精品欧美一区二区三区久久久 | 天堂综合网久久 | 精品免费国产视频 | 国产成人综合久久 | 色婷婷影院 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 午夜精品一区二区三区在线观看 | 欧美.com| 精品福利一区二区三区 | 综合久久综合久久 | 亚洲黄色国产 | 在线观看亚洲欧美 | 日韩91| 99久久精品免费 | 色综合激情 | 久久九九免费 | 日韩高清中文字幕 | 91久久国产综合久久91精品网站 | 情侣酒店偷拍一区二区在线播放 | av在线影院 | 啪啪免费网 | 国产色网站 | 成年人免费看的视频 | 午夜精品久久久久久久久久久久 | 国产免费拔擦拔擦8x高清 | 伊人网站在线观看 | 91精品国产综合久久福利软件 | 亚洲日本视频 | 国产欧美日韩在线一区 | 久久成人免费视频 | 成人字幕网zmw | 国产成人免费视频 | 成人免费观看视频 | 亚洲乱码一区二区三区在线观看 | 亚洲精品电影在线观看 | 久久久久久久久一区 | 色视频成人在线观看免 | av 一区二区三区 |