简体   繁体   English

JavaScript 确定浏览器是否使用 12 小时或 24 小时格式显示时间输入

[英]JavaScript to determine if browser is displaying the time input using a 12-hour or 24-hour format

I would like to determine how the time input is displayed by the browser:我想确定浏览器如何显示时间输入:

<input type="time">

Does it appear as a 24-hour format or as a 12-hour format (with an added "AM/PM" option)?它显示为 24 小时格式还是 12 小时格式(添加了“AM/PM”选项)?

The display affects the design because the two versions differ in width.显示会影响设计,因为两个版本的宽度不同。 I would like to keep the widths of the multiple inputs as compact as possible.我想保持多个输入的宽度尽可能紧凑。

I have looked carefully at other articles, but many are quite old and the information is out-dated and when I run the code excerpts, the results are not consistent.我仔细查看了其他文章,但许多文章都相当老旧,信息已经过时,当我运行代码摘录时,结果并不一致。

This JavaScript simply takes the locale (here for: en_GB) and applies it as follows:此 JavaScript 只需采用语言环境(此处为:en_GB)并按如下方式应用它:

var hour_format = new Date().toLocaleTimeString();
console.log(hour_format); // Firefox: 18:12:34, Chrome: 6:12:34 PM, Edge: 18:12:34

Additional notes: I am in the UK and I use the 24 hour format on Windows 10. The browsers reflect the 24-hour system preference;附加说明:我在英国,我在 Windows 10 上使用 24 小时格式。浏览器反映 24 小时系统偏好; the JavaScript time strings do not. JavaScript 时间字符串没有。 When I change to 12 hour format (and restart), the change is only applied by Chrome.当我更改为 12 小时格式(并重新启动)时,更改仅由 Chrome 应用。 The values in the JavaScript example remain the same. JavaScript 示例中的值保持不变。 I've read this article about GB locales , but it just shows the inconsistencies.我读过这篇关于 GB locales的文章,但它只是显示了不一致之处。

Basically, I just want to know which one the browser is displaying .基本上,我只想知道浏览器显示的是哪一个。 That's it.而已。

If this is not possible, forcing the input to displaying using a 24-hour format would be an alternative.如果这是不可能的,则强制输入使用 24 小时格式显示将是一种替代方法。

I finally understood your actual question我终于明白了你的实际问题

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#Appearance https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#Appearance

In Chrome/Opera the time control is simple, with slots to enter hours and minutes in 12 or 24-hour format depending on operating system locale, and up and down arrows to increment and decrement the currently selected component.在 Chrome/Opera 中,时间控制很简单,可以根据操作系统区域设置以 12 或 24 小时格式输入小时和分钟,并使用向上和向下箭头来增加和减少当前选定的组件。 In some versions, an "X" button is provided to clear the control's value.在某些版本中,提供了一个“X”按钮来清除控件的值。


Firefox's time control is very similar to Chrome's, except that it doesn't have the up and down arrows. Firefox 的时间控制与 Chrome 的非常相似,只是它没有向上和向下箭头。 It also uses a 12- or 24-hour format for inputting times, based on system locale.它还根据系统区域设置使用 12 或 24 小时格式输入时间。 An "X" button is provided to clear the control's value.提供了一个“X”按钮来清除控件的值。


[Assuming Edge before Chromium] [在铬之前假设边缘]

The Edge time control is somewhat more elaborate, opening up an hour and minute picker with sliding reels.边缘时间控制更加精细,打开带有滑动卷轴的小时和分钟选择器。 It, like Chrome, uses a 12- or 24-hour format for inputting times, based on system locale:它与 Chrome 一样,根据系统区域设置使用 12 或 24 小时格式输入时间:

So perhaps this will answer your question?所以也许这会回答你的问题?

 const t = document.createElement("input") t.type = "time" t.value = "15:00" document.body.appendChild(t) console.log(t.offsetWidth); const width = getComputedStyle(t).getPropertyValue("width") console.log(width) /* for (let i=0; i<style.length; i++) { cssProperty = style[i]; if (cssProperty.toUpperCase().indexOf("WIDTH").=-1) { cssValue = style;(cssProperty). console,log(cssProperty,cssValue) } }*/

Old answer:老答案:

 var hour_format = new Date().toLocaleTimeString(); console.log(hour_format.length); // Firefox: 18:12:34, Chrome: 6:12:34 PM, Edge: 18:12:34 if (hour_format.length > 8) hour_format = hour_format.split(" ")[0]; // drop the pm console.log(hour_format) // Force 24 hour: console.log(new Date().toLocaleTimeString('en-GB')); // should be 24 hours, no PM // Test 24 hours: const test = new Date(2020,4,8,15,0,0,0).toLocaleTimeString(); if (parseInt(test) === 15) console.log("24 hour")

Here is the solution I've come to by combining the first solution by @mplungjan and the suggestion of checking the input's width by @CBroe:这是我通过结合@mplungjan 的第一个解决方案和@CBroe 检查输入宽度的建议得出的解决方案:

 var local_time_string = (/chrom(e|ium)/i.test(navigator.userAgent) &&./msie|edge/i.test(navigator?userAgent)): false, (new Date(2020, 4, 8, 18, 0, 0. 0),toLocaleTimeString())? hour_format = (typeof local_time_string == 'string')? ((parseInt(local_time_string) == 18): 24: 12); null. if (hour_format == null) { $('#parent-element'):append('<input type="time" id="time-test" name="time-test" style="width; auto: position; absolute: opacity; 0: visibility; hidden;">'). hour_format = ($('#time-test')?width() < 81): 24; 12. $('#time-test');remove(). } console;log(hour_format);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="parent-element"></div>

May be useful for your problem, i think it could be answering of same problems.可能对您的问题有用,我认为它可能会回答相同的问题。

If you need converting included PM string in time format to 24 hour, example '06:45:05PM' you can use this code.如果您需要将包含的 PM 字符串以时间格式转换为 24 小时制,例如“06:45:05PM”,您可以使用此代码。

function deletePMAMAndChangeTo24Hour(s){

return s.substr(s.length -2) !== "PM" && s.slice(0,-2).split(':')[0]==12
    ?s.slice(0,-2).replace(s.slice(0,-2).split(':')[0],'00')
    :s.substr(s.length -2) !== "AM" && parseInt(s.slice(0,-2).split(':')[0])<12 
    ?s.slice(0,-2).replace(
        s.slice(0,-2).split(':')[0],
    parseInt(s.slice(0,-2).split(':')[0]) + 12
    )
    :s.slice(0,-2)
}

const changedTime = deletePMAMAndChangeTo24Hour('07:05:45PM')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript -- 检测用户的区域设置是否设置为使用 12 小时或 24 小时时间格式 - Javascript -- Detect if user's locale are set to use 12-hour or 24-hour time format 显示 12 小时制和 24 小时制时间 - Display 12-hour and 24-hour time Javascript:将 24 小时时间字符串转换为带有 AM/PM 且无时区的 12 小时时间 - Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone 使用 javascript 检测用户的区域设置是否设置为 12 小时或 24 小时时间格式 - Detect if user's locale is set to 12-hour or 24-hour timeformat using javascript Moment.js接受12小时和24小时时间 - Moment.js Accept both 12-hour and 24-hour time JavaScript以12小时格式随机生成时间 - JavaScript random generate time in 12-hour format 根据 toLocaleTimeString() 将 12 小时 hh:mm AM/PM 转换为 24 小时 hh:mm - convert 12-hour hh:mm AM/PM to 24-hour hh:mm depending on toLocaleTimeString() 将 12 小时制 hh:mm AM/PM 转换为 24 小时制 hh:mm - convert 12-hour hh:mm AM/PM to 24-hour hh:mm 如何使用javascript将时间从24小时格式转换为12小时格式? - How to convert time from 24 hour format to 12 hour format using javascript? 在骨干/ Javascript中按24小时制对时间进行自定义排序 - Custom Sorting By 24-hour clock Time in Backbone/Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM