@Marko Bonaci 的回答很有帮助。我研究了他的评论“我不确定 AM/PM 名称(无论使用哪个字符)是否位于所有语言环境的输出末尾。”列出了 Google 搜索这个链接 https://spanishdict.com/answers/174077/when-keeping-time-how-do-you-translate-am-morning-and-pm-evening陈述用户的评论:
拉丁文缩写 a.m. 和 p.m.
(通常写为“am”和“pm”、“AM”和“PM”或“A.M.”和“P.M.”)
用于英语、葡萄牙语(巴西)和西班牙语。
希腊语中的等价物是 π.μ。和μ.μ。分别。
这可以使用两个字母从浏览器的控制台进行验证ISO 639-1 https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes代码“el”或三个字母的 ISO 639-2 代码“ell”:
new Intl.DateTimeFormat(["el"], { hour: "numeric" }).format();
// or
new Intl.DateTimeFormat("ell", { hour: "numeric" }).format();
这些行将返回带有本地化“AM”/“PM”字符串的值:
'7 π.μ.'
'7 μ.μ.'
我最终使用了他的建议,使用内置的 'Number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number' JavaScript 中的对象。
// UK english
Number.isInteger(Number(new Intl.DateTimeFormat("en-UK", { hour: "numeric" }).format()));
// Returns 'true'
// Greek
Number.isInteger(Number(new Intl.DateTimeFormat("el", { hour: "numeric" }).format()));
// Returns 'false'
// U.S.
Number.isInteger(Number(new Intl.DateTimeFormat("en-US", { hour: "numeric" }).format()));
// Returns 'false'
这是我的功能和评论:
const isBrowserLocaleClockType24h = (languages) => {
// "In basic use without specifying a locale, DateTimeFormat
// uses the default locale and default options."
// Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_datetimeformat
// To be sure of the browser's language (set by the user
// and may be different than the operating system's default language)
// set the 'languages' parameter to 'navigator.language'.
// E.g. isBrowserLocaleClockType24h(navigator.language);
if (!languages) { languages = []; }
// The value of 'hr' will be in the format '0', '1', ... up to '24'
// for a 24-hour clock type (depending on a clock type of
// 'h23' or 'h24'. See:
// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale
// Intl.Locale.prototype.hourCycles
// Returns an Array of hour cycle identifiers, indicating either
// the 12-hour format ("h11", "h12") or
// the 24-hour format ("h23", "h24").
// A 12-hour clock type value has the format '7 AM', '10 PM', or
// '7 π.μ.' (if the locale language is Greek as specified
// by the two letter ISO 639-1 code "el" or
// three letter ISO 639-2 code "ell").
const hr = new Intl.DateTimeFormat(languages, { hour: "numeric" }).format();
// If there's no space in the value of the 'hr' variable then
// the value is a string representing a number between and
// can include '0' and '24'. See comment above regarding "hourCycles".
// Return 'true' if a space exists.
//if (!hr.match(/\s/)) { return true; }
// Or simply:
// return !hr.match(/\s/);
// Alternatively, check if the value of 'hr' is an integer.
// E.g. Number.isInteger(Number('10 AM')) returns 'false'
// E.g. Number.isInteger(Number('7 π.μ.')) returns 'false'
// E.g. Number.isInteger(Number('10')) returns 'true'
return Number.isInteger(Number(hr));
};
Usage:
const isBrowserLocaleClock24h = isBrowserLocaleClockType24h();
// or
const isBrowserLocaleClock24h = isBrowserLocaleClockType24h(navigator.language);
使用 Intl.Locale().hourCycles
最后,对于较新的浏览器(截至 2022 年 12 月的 Firefox 除外),新的 Intl.Locale(navigator.language).hourCycles 列出在MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycles.
// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#checking_whether_a_value_exists_in_an_array
const hourCycles = new Intl.Locale(navigator.language).hourCycles;
const isBrowserLocale24h = ["h23", "h24"].some(hourCycle => hourCycles.includes(hourCycle));
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/hourCycle
Intl.Locale.prototype.hourCycle 属性是一个访问器属性,它返回区域设置使用的时间保持格式约定。
世界各地使用的计时惯例(时钟)有两种主要类型:12 小时制和 24 小时制。 hourCycle 属性使 JavaScript 程序员可以更轻松地访问特定区域设置使用的时钟类型。