如何在Chrome中检查系统的时间格式-使用JavaScript是12个小时还是24个小时? [英] How to check system's time format in chrome - 12 hours or 24 hours in JavaScript?

查看:329
本文介绍了如何在Chrome中检查系统的时间格式-使用JavaScript是12个小时还是24个小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

date.toLocaleTimeString在chrome中不起作用,并且始终以12小时格式返回时间.我需要根据系统的时间格式显示时间.

date.toLocaleTimeString does not work in chrome and always return time in 12 hours format. I need to display time on the basis of system's time format.

推荐答案

简短的回答是,您无法在浏览器中获取默认的时间格式,因为它依赖于系统以及浏览器设置,以为JavaScript无法访问此类选项.

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.

但是,如果要在代码中管理时间格式,则可以指定用于显示日期的格式.

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.

实际上 toLocaleString( ) 有一个布尔值hour12选项,该选项告诉引擎使用还是不使用12 hours格式:

Actually the toLocaleString() have a boolean hour12 option that tells the engine to use or not the 12 hours format:

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

如果将其设置为false,它将以24 hours格式显示时间.

If you set it to false it will display the time in 24 hours format.

这篇关于如何在Chrome中检查系统的时间格式-使用JavaScript是12个小时还是24个小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆