在JavaScript设置一个语言环境 [英] Set a locale in javascript
问题描述
的jQuery通过测试的日期的有效性:
jQuery tests the validity of a date via:
!/Invalid|NaN/.test(new Date(value))
新的日期(dateString)
相同 Date.parse(dateString)
并采用浏览器/操作系统的语言环境解析字符串。
new Date(dateString)
is the same as Date.parse(dateString)
and uses browser/OS locale to parse the string.
我试图解析 DD / MM / YYYY
,而是因为我的浏览器正在寻找 MM / DD / YYYY我得到一个错误
。由于我的产品将被 DD / MM
人只使用,我想迫使这一行为。
I'm trying to parse DD/MM/YYYY
but I get an error because my browser is looking for MM/DD/YYYY
. Since my product will be used only by DD/MM
people, I want to force this behaviour.
我可以写一个定制的验证,但有可能通过JavaScript改变浏览器的语言环境中呢?
I could write a custom validator, but is it possible to change the browser locale via JavaScript as well?
推荐答案
您不能更改区域,因为在EMCAScript的规范。
You can't change the locale, as there is no such feature in the EMCAScript specification.
不过,有一个好的包 php.js 是在JavaScript中实现PHP函数。
However, there is a nice package called php.js that implements PHP functions in JavaScript.
的功能的两个都的setlocale ()和日期()。你可以使用它们。
Two of the functions are setlocale() and date(). You can use them.
这篇关于在JavaScript设置一个语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!