简体   繁体   English

如何检查Chrome中系统的时间格式-使用JavaScript是12个小时还是24个小时?

[英]How to check system's time format in chrome - 12 hours or 24 hours in JavaScript?

date.toLocaleTimeString does not work in chrome and always return time in 12 hours format. date.toLocaleTimeString在chrome中不起作用,并且始终以12小时格式返回时间。 I need to display time on the basis of system's time format. 我需要根据系统的时间格式显示时间。

Use 采用

var x = date() 

// Tue May 15 2012 05:45:40 GMT-0500

They already provide you the datetime with the timezone 他们已经为您提供了带有时区的日期时间

OR 要么

To make your life easier, use this 为了使您的生活更轻松,请使用此

 https://momentjs.com/

Short answer is No you can't get the default time format in the browser, because it relies on the System and on the browser settings, thought JavaScript doesn't have access for such options. 简短的答案是: 否,您不能在浏览器中获得默认的时间格式,因为它依赖于系统和浏览器设置,因为JavaScript无法访问此类选项。

But if you want to manage the time format within your code, you can specify the format by which you wnat to show your Date. 但是,如果您想在代码中管理时间格式,则可以指定用于显示日期的格式。

Actually the toLocaleString() have a boolean hour12 option that tells the engine to use or not the 12 hours format: 实际上, toLocaleString()具有boolean hour12选项,该选项告诉引擎使用或不使用12 hours格式:

console.log(date.toLocaleString('en-US', { hour12: false }));

If you set it to false it will display the time in 24 hours format. 如果将其设置为false ,它将以24 hours格式显示时间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM