JavaScript中的日期解析在safari和chrome之间是不同的 [英] Date parsing in javascript is different between safari and chrome

查看:305
本文介绍了JavaScript中的日期解析在safari和chrome之间是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

var c = new Date(Date.parse("2011-06-21T14:27:28.593Z"));
console.log(c);

在Chrome上,它正确打印出控制台上的日期。在Safari
中,它失败。谁是正确的,更重要的是什么是最好的方式
来处理这个?

On Chrome it correctly prints out the date on the console. In Safari it fails. Who is correct and more importantly what is the best way to handle this?

推荐答案

你不能真正使用Date.parse。我建议你使用: new Date(year,month [,date [,hours [,minutes [,seconds [,ms]]]]))

You can't really use Date.parse. I suggest you use: new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )

要分割字符串可以尝试

var s = '2011-06-21T14:27:28.593Z';
var a = s.split(/[^0-9]/);
//for (i=0;i<a.length;i++) { alert(a[i]); }
var d=new Date (a[0],a[1]-1,a[2],a[3],a[4],a[5] );
alert(s+ " "+d);

这篇关于JavaScript中的日期解析在safari和chrome之间是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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