JavaScript 中不同浏览器的日期格式问题

发布于 2024-09-15 14:25:31 字数 359 浏览 2 评论 0原文

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

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

Firefox 对此很满意,但 Chrome 和 IE 返回无效日期。

我想我应该尝试如下替换 T 和 Z:

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

这次 Chrome 对此感到满意,但 Firefox 和 IE 返回无效日期。

有什么想法我应该做什么才能在所有浏览器中使用这种格式获取日期对象?

非常感谢, 蒂姆

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 is happy with that, but Chrome and IE return Invalid Date.

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

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

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?!

Many thanks,
Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伊面 2024-09-22 14:25:31

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

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

IE8

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

、FF3.6、Safari4、Chrome

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

长伴 2024-09-22 14:25:31

您还可以尝试使用 Date.js - 一个开源 JavaScript 日期操作库。

You could also try using Date.js - an open source javascript date manipulation library.

迷爱 2024-09-22 14:25:31

你能尝试一下吗:

new Date(2001,0,1,12,0,0)

这意味着:

new Date(year,month,day,hour,minutes,seconds) 

Can you try:

new Date(2001,0,1,12,0,0)

This means:

new Date(year,month,day,hour,minutes,seconds) 
千纸鹤带着心事 2024-09-22 14:25:31

这适用于所有主要的 5 个浏览器,并使所有浏览器将时间识别为 GMT/UTC 而不是本地时间(Z 后缀表示时间是 UTC):

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

我感谢 mplungjan 的回答。

This works on all of the major 5 browsers and causes all browsers to recognize the time as GMT/UTC rather than local time (the Z suffix means the time is UTC):

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

I thank mplungjan for his answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文