new Date() 和 split() 跨浏览器兼容性
我正在尝试将日期显示为 Fri, Feb 17, 2012
而不是 Friday, February 17, 2012
这是我的代码:
var n = new Date(),
ampm = 'am',
h = n.getHours(),
m = n.getMinutes(),
s = n.getSeconds();
if (h >= 12) {
if (h > 12) h -= 12;
ampm = 'pm'
}
if (h < 10) h = '0' + h;
if (m < 10) m = '0' + m;
t = n.toLocaleDateString();
var d =
t.split(',')[0].substring(0, 3)
+ ', '
+ t.split(',')[1].substring(1, 4)
+ ' '
+ t.split(',')[1].split(' ')[2]
+ t.split(',')[2]
+ ', '
+ ' '
+ h + ':' + m + ' ' + ampm;
var e = Math.round((new Date() - new Date('2012/01/01').getTime()) / 1000);
var f = [1];
var a = [];
var i = 0;
alert(d);
现在这适用于某些浏览器,但是但对其他人却没有影响。有的浏览器显示17、201、undefined未定义,有的浏览器显示我要找的内容。
谁能发现我缺少什么吗?
小提琴: http://jsfiddle.net/nxyum/
适用于:
Windows: Chrome, Safari
Mac: Chrome, Opera
不起作用:
Windows: Firefox, Opera, IE
Mac: Firefox, Safari
I'm trying to make a date to appear as Fri, Feb 17 2012
instead of Friday, February 17, 2012
Here is my code:
var n = new Date(),
ampm = 'am',
h = n.getHours(),
m = n.getMinutes(),
s = n.getSeconds();
if (h >= 12) {
if (h > 12) h -= 12;
ampm = 'pm'
}
if (h < 10) h = '0' + h;
if (m < 10) m = '0' + m;
t = n.toLocaleDateString();
var d =
t.split(',')[0].substring(0, 3)
+ ', '
+ t.split(',')[1].substring(1, 4)
+ ' '
+ t.split(',')[1].split(' ')[2]
+ t.split(',')[2]
+ ', '
+ ' '
+ h + ':' + m + ' ' + ampm;
var e = Math.round((new Date() - new Date('2012/01/01').getTime()) / 1000);
var f = [1];
var a = [];
var i = 0;
alert(d);
Now this works on some browsers but it doesnt on others. Some browsers show 17, 201, undefinedundefined, and some browsers show what I am looking for.
Can anyone spot what I am missing?
Fiddle: http://jsfiddle.net/nxyum/
Works on:
Windows: Chrome, Safari
Mac: Chrome, Opera
Doesn't work:
Windows: Firefox, Opera, IE
Mac: Firefox, Safari
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白你在用你的代码做什么......很容易输出一个你喜欢的任何格式的日期 -
I don't see what you are doing with your code...It's easy to output a date formatted any way you like-