使用不同浏览器的JavaScript中的日期格式问题 [英] Problem with date formats in JavaScript with different browsers

查看:91
本文介绍了使用不同浏览器的JavaScript中的日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RSS提要中的日期,但在IE,Chrome和Firefox中使用以下代码时,会发现不同的结果:

I am working with dates in an RSS feed, but am finding differing results when using the code below in IE, Chrome and Firefox:

new Date('2001-01-01T12:00:00Z')

Firefox是很高兴,但Chrome和IE返回无效日期。

Firefox is happy with that, but Chrome and IE return Invalid Date.

我以为我会尝试用T和Z替换如下:

I thought I'd try replacing the T and Z as follows:

new Date('2001-01-01 12:00:00')

这次Chrome很高兴,但Firefox和IE返回无效日期。

This time Chrome is happy with that, but Firefox and IE return Invalid Date.

任何想法我应该做什么来获得所有浏览器中的日期对象具有此格式?

Any ideas what I should do to get a date object in all browsers with this format?!

非常感谢,
Tim

Many thanks, Tim

推荐答案

这可以在我的盒子上的所有浏览器中使用 - 在控制台中尝试:

This works in all browsers on my box - try it in the console:

alert(new Date('2001/01/31 12:00:00'))

so

new Date('2001-01-01T12:00:00Z'.replace(/\-/g,'\/').replace(/[T|Z]/g,' '))

IE8,FF3.6, Safari4,Chrome

IE8, FF3.6, Safari4, Chrome

这篇关于使用不同浏览器的JavaScript中的日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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