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

查看:26
本文介绍了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 [, minute [, seconds [, ms ] ] ] ] )

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

要拆分字符串,您可以尝试

To split the string you could try

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天全站免登陆