Javascript-检索操作系统日期格式 [英] Javascript - Retrieve OS Date formatting

查看:31
本文介绍了Javascript-检索操作系统日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯...好吧,我已经在网上搜索了2天,没有任何运气.我已经看到了很多有关如何格式化JavaScript日期的答案,例如new Date().toString("yyyy-MM-dd")...它将返回类似2013-04-05的信息.

Hmmm... Okay so I've been searching the web for 2 days now without any luck. I've seen a lot of answers on how to format a javascript date for example new Date().toString("yyyy-MM-dd")... Which would return something like 2013-04-05.

这绝对不是问题.

我想要的是可以设置操作系统显示日期的格式,然后检索该特定格式并将其显示在浏览器中.

What I want, is the possibility to set the format in which my OS displays dates, then retrieve that specific format and display it in the browser.

例如,假设我将操作系统中的日期格式更改为MM-yyyy/dd(这是出于争论的目的,无论是否可行都无关紧要).然后,我希望在自己的操作系统中看到04-2013/05,对吗?接下来,我想通过Javascript在浏览器中检索此特定格式,以便可以使用它来格式化整个网页中的日期.

For example, let's say I changed the format of the date in my OS to MM-yyyy/dd (this is for arguement sakes, whether that would work or not is irrelevant)). Then I'd expect to see 04-2013/05 in my OS, right? Next I want to retrieve this specific format in my browser via Javascript so that I can use this to format my dates throughout my webpage.

如果这是荒谬的事情并且无法完成,请告诉我,因为我因搜索而感到头疼.另外,如果您说使用 someDateObject .toLocaleDateString()而不确切说明.toLocaleDateString为何起作用,我将忽略它,因为我尝试将OS中的日期格式设置为无数格式,并且每次使用.toLocaleDateString()时,都会收到相同的格式:dd/MM/yyyy

If this is lunacy and cannot be done, please tell me, as I've got a headache from searching. Also, if you say use someDateObject.toLocaleDateString() without explaining exactly why .toLocaleDateString would work, I'm going to ignore it, because I've tried setting my date-format in my OS to numerous formats and every single time I use .toLocaleDateString(), I receive the same format: dd/MM/yyyy

推荐答案

使用的.toLocaleDateString方法语言环境的第一个属性.

first attribute of .toLocaleDateString method locale(s) used.

当前语言环境.在IE中,您可以获取navigator.browserLanguage或navigator.systemLanguage

current locale you can obtain through navigator.language (in firefox or chrome) parameter. in IE you can obtain navigator.browserLanguage or navigator.systemLanguage

在IE以外的浏览器中,无法以这种方式获取系统语言

in browsers other than IE it is impossible to obtain system language this way

此后,您可以调用新的Date.toLocaleString(navigator.language || navigator.browserLanguage),它将根据浏览器语言正确设置格式

after this you can call new Date.toLocaleString(navigator.language||navigator.browserLanguage) and it will be formated correctly depending on browser language

这篇关于Javascript-检索操作系统日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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