来自 ISO8061 的 Javascript 时间戳
我在处理从 iso8061 日期获取时间戳时遇到了一些问题。 由于某种原因,它在 Chrome 中完美运行,但在 Firefox 中导致无效日期错误。确切的行是:
var date = new Date(time.replace(/-/g,"/").replace(/[TZ]/g," "));
我尝试通过(作为 var 时间)2011-03-09T16:46:58+00:00
、2011-03-09T16:46 传递日期:58+0000
和 2011-03-09T16:48:37Z
根据规范概述 http://www.jibbering.com/faq/#dates 但我似乎仍然无法让它在 Firefox 中工作。事实上,最后一种方法在这两种浏览器中都不起作用。
如果有人可以帮助我将这个 iso8061 日期转换为时间戳,那就太好了。
谢谢, 安吉洛·R。
I'm having a bit of an issue when dealing with getting a timestamp from an iso8061 date.
For some reason it work perfectly in Chrome, but causes an Invalid Date error in Firefox. The exact line is:
var date = new Date(time.replace(/-/g,"/").replace(/[TZ]/g," "));
I've tried passing the date through (as the var time) 2011-03-09T16:46:58+00:00
, 2011-03-09T16:46:58+0000
and 2011-03-09T16:48:37Z
as per the spec outlined http://www.jibbering.com/faq/#dates but I still can't seem to get it to work in firefox. In fact, the last method didn't work in either browser.
If anyone could help me turn this iso8061 date into a timestamp, that would be great.
Thanks,
Angelo R.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 JavaScript ISO8601/RFC3339 Date Parser:
他们的代码:
然后你就可以以这种方式使用它:
可能不太舒服,但您可以重写此函数,或者编写另一个函数,该函数将返回基于 ISO-8601 格式的日期
take a look at JavaScript ISO8601/RFC3339 Date Parser:
their code:
and then you can use it this way:
probably not that comfortable, but you can rewrite this function, or write another one which will return date based on ISO-8601 format
Date
构造函数处理字符串参数的方式因浏览器而异。正如 这个问题 的第一个答案指出的,IE 可以识别连字符,但 Firefox 不能,仅举一个例子。最好使用需要单独日期部分的构造函数。
The way that the
Date
constructor handles string arguments differs across browsers. As the first answer to this question points out, IE recognizes hyphens, but Firefox does not, as just one example.It's probably best to use the constructor that expects individual date parts.