使用 javascript 检测用户的区域设置是否设置为 12 小时或 24 小时时间格式

JAG*_*GAN 3 javascript browser time-format datetime-format

如何使用 Javascript 检查用户是否使用 12 小时或 24 小时时间格式,无论是否使用像 moment.js 这样的第三方库我也尝试过,new Date().toLocaleString()但它没有在 Firefox 和 google chrome 中进行测试。firefox 始终显示 12 小时格式,chrome 始终显示 24 小时时间格式

Dav*_*e B 6

@Marko Bonaci 的回答很有帮助。我研究了他的评论“我不确定 AM/PM 名称(无论使用哪个字符)是否位于所有语言环境的输出末尾。” Google 搜索列出了此链接,说明了用户的评论:

\n
\n

拉丁语缩写 am 和 pm\n(通常写为“am”和“pm”、“AM”和“PM”或“AM”和“PM”)\n用于英语、葡萄牙语(巴西)和西班牙语。\ n希腊语中的等价物是 \xcf\x80.\xc2\xb5。和 \xc2\xb5.\xc2\xb5。分别。

\n
\n

这可以使用两个字母ISO 639-1从浏览器的控制台进行验证代码“el”或三个字母的 ISO 639-2 代码“ell”

\n
new Intl.DateTimeFormat(["el"], { hour: "numeric" }).format();\n// or\nnew Intl.DateTimeFormat("ell", { hour: "numeric" }).format();\n
Run Code Online (Sandbox Code Playgroud)\n

这些行将返回带有本地化“AM”/“PM”字符串的值:

\n
\'7 \xcf\x80.\xce\xbc.\'\n\'7 \xce\xbc.\xce\xbc.\'\n
Run Code Online (Sandbox Code Playgroud)\n

我最终采用了他的建议,使用 JavaScript 中的内置“ Number ”对象。

\n
// UK english\nNumber.isInteger(Number(new Intl.DateTimeFormat("en-UK", { hour: "numeric" }).format()));\n// Returns \'true\'\n\n// Greek\nNumber.isInteger(Number(new Intl.DateTimeFormat("el", { hour: "numeric" }).format()));\n// Returns \'false\'\n\n// U.S.\nNumber.isInteger(Number(new Intl.DateTimeFormat("en-US", { hour: "numeric" }).format()));\n// Returns \'false\'\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的带有注释的函数:

\n
const isBrowserLocaleClockType24h = (languages) => {\n    // "In basic use without specifying a locale, DateTimeFormat\n    // uses the default locale and default options."\n    // Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_datetimeformat\n    // To be sure of the browser\'s language (set by the user\n    // and may be different than the operating system\'s default language)\n    // set the \'languages\' parameter to \'navigator.language\'.\n    // E.g. isBrowserLocaleClockType24h(navigator.language);\n    if (!languages) { languages = []; }\n\n    // The value of \'hr\' will be in the format \'0\', \'1\', ... up to \'24\'\n    // for a 24-hour clock type (depending on a clock type of\n    // \'h23\' or \'h24\'. See:\n    // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale\n    // Intl.Locale.prototype.hourCycles\n    // Returns an Array of hour cycle identifiers, indicating either\n    // the 12-hour format ("h11", "h12") or\n    // the 24-hour format ("h23", "h24").\n\n    // A 12-hour clock type value has the format \'7 AM\', \'10 PM\', or\n    // \'7 \xcf\x80.\xce\xbc.\' (if the locale language is Greek as specified\n    // by the two letter ISO 639-1 code "el" or \n    // three letter ISO 639-2 code "ell").\n\n    const hr = new Intl.DateTimeFormat(languages, { hour: "numeric" }).format();\n\n    // If there\'s no space in the value of the \'hr\' variable then\n    // the value is a string representing a number between and\n    // can include \'0\' and \'24\'. See comment above regarding "hourCycles".\n    // Return \'true\' if a space exists.\n    //if (!hr.match(/\\s/)) { return true; }\n    // Or simply:\n    // return !hr.match(/\\s/);\n\n    // Alternatively, check if the value of \'hr\' is an integer.\n    // E.g. Number.isInteger(Number(\'10 AM\')) returns \'false\'\n    // E.g. Number.isInteger(Number(\'7 \xcf\x80.\xce\xbc.\')) returns \'false\'\n    // E.g. Number.isInteger(Number(\'10\')) returns \'true\'\n    return Number.isInteger(Number(hr));\n};\n
Run Code Online (Sandbox Code Playgroud)\n

用法:

\n
const isBrowserLocaleClock24h = isBrowserLocaleClockType24h();\n// or\nconst isBrowserLocaleClock24h = isBrowserLocaleClockType24h(navigator.language);\n
Run Code Online (Sandbox Code Playgroud)\n

使用 Intl.Locale().hourCycles

\n

最后,对于较新的浏览器(截至 2022 年 12 月的 Firefox 除外),MDN上列出了新的 Intl.Locale(navigator.language).hourCycles 。

\n
// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#checking_whether_a_value_exists_in_an_array\nconst hourCycles = new Intl.Locale(navigator.language).hourCycles;\nconst isBrowserLocale24h = ["h23", "h24"].some(hourCycle => hourCycles.includes(hourCycle));\n
Run Code Online (Sandbox Code Playgroud)\n

\n
\n

Intl.Locale.prototype.hourCycle 属性是一个访问器属性,它返回区域设置使用的时间保持格式约定。

\n
\n
\n

世界各地使用的计时惯例(时钟)有两种主要类型:12 小时制和 24 小时制。hourCycle 属性使 JavaScript 程序员可以更轻松地访问特定区域设置使用的时钟类型。

\n
\n