如何根据浏览器文化格式化 JavaScript 日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8469436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How format JavaScript Date with regard to the browser culture?
提问by IT Hit WebDAV
I need to format the JavaScript Date object. It should display date and time in short format with regard to the culture that browser is using, for example:
我需要格式化 JavaScript Date 对象。它应该根据浏览器使用的文化以短格式显示日期和时间,例如:
20/10/2011 10:20 PM
10.20.2011 22:20
How do I do this (preferably with jQuery)?
我该怎么做(最好使用 jQuery)?
回答by themerlinproject
The Date() object will get you the client time (live demo here):
Date() 对象将为您提供客户端时间(此处为现场演示):
var now=new Date();
alert(now.toLocaleString());
JS and jQuery don't have a built in format function. To format it differently use a function like format()
(1.2kb) hereand then the following code will produce a format like "10/10/2012 8:50pm" :
JS 和 jQuery 没有内置格式功能。要对其进行不同的格式化,请在此处使用类似format()
(1.2kb)的函数,然后以下代码将生成类似 "10/10/2012 8:50pm" 的格式:
var now=new Date();
alert( now.format("mm/dd/yy h:MM:ss TT") );
回答by RobG
Browsers either use system settings for date formats, or use their own (often US–centric) settings.
浏览器要么使用日期格式的系统设置,要么使用他们自己的(通常以美国为中心)设置。
There is a Date.prototype.toLocaleDateString()method that should return a date based on current system settings, however it's implementation dependent and completely unreliable due to being inconsistent between browsers.
有一个Date.prototype.toLocaleDateString()方法应该根据当前系统设置返回一个日期,但是由于浏览器之间的不一致,它依赖于实现并且完全不可靠。
e.g. for me on 13 December, 2011:
例如对我来说,2011 年 12 月 13 日:
- Safari returns
13 December 2001
- Firefox
12/13/2011
- Opera
Tuesday December 13, 2011
- Chrome
Tuesday, December 13, 2011
- IE 6
Tuesday, 13 December, 2011
- Safari 回归
13 December 2001
- 火狐
12/13/2011
- 歌剧
Tuesday December 13, 2011
- 铬合金
Tuesday, December 13, 2011
- 浏览器 6
Tuesday, 13 December, 2011
So only Safari and IE actually use the system settings, it seems the developers of other browsers are either too lazy, indifferent or ignorant to accommodate non–US users.
所以实际上只有Safari和IE使用了系统设置,看来其他浏览器的开发者要么是懒惰,要么是无知,要么是无知,无法容纳非美国用户。
An alternative is to either ask the user what format they prefer, or just use an unambiguous format, e.g. 13-Dec-2011 will be understood by everyone. If you really must use numbers only, then the ISO-8601 format should do fine: 2011-12-13 with the added benefit that it is simple to sort.
另一种方法是询问用户他们喜欢哪种格式,或者只使用明确的格式,例如 13-Dec-2011 将被每个人理解。如果您真的必须只使用数字,那么 ISO-8601 格式应该可以: 2011-12-13 具有易于排序的额外好处。
Some functions that print a short date in the above formats:
一些以上述格式打印短日期的函数:
// format: 2011/12/5
function shortDate1(obj) {
obj = obj || new Date();
return obj.getFullYear() + '/' + (obj.getMonth() + 1) + '/' + obj.getDate();
}
// format: 2011/12/05
// (padded single digit month and day)
function shortDate2(obj) {
function z(n) {
return (n < 10? '0' : '') + n;
}
obj = obj || new Date();
return obj.getFullYear() + '/' + z(obj.getMonth() + 1) + '/' + z(obj.getDate());
}
// format: 15-Dec-2011
function shortDate3(obj) {
obj = obj || new Date();
function z(n) {
return (n < 10? '0' : '') + n;
}
months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
return [z(obj.getDate()),months[obj.getMonth()],obj.getFullYear()].join('-');
}
回答by kennebec
It's handy to know if a system uses day-month or month-day in its string methods, mostly to set the order of user inputs. toLocaleString is fine for displayinga known date- and it is international.
知道系统在其字符串方法中是使用 day-month 还是 month-day 很方便,主要是为了设置用户输入的顺序。toLocaleString 适合显示已知日期 - 它是国际化的。
Date.dmOrder=(function(){
return Date.parse('2/6/2009')> Date.parse('6/2/2009');
})()
if(Date.dmOrder)// ask for the date first
else // ask for the month first