在Javascript中解析Youtube API日期 [英] Parsing a Youtube API Date in Javascript

查看:131
本文介绍了在Javascript中解析Youtube API日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Youtube的API返回一个包含视频数组的JSON对象。每个视频对象的发布日期格式如2012-01-11T20:49:59.415Z。如果我使用下面的代码初始化一个Javascript Date对象,该对象返回无效日期。

Youtube's API returns a JSON object with an array of videos. Each video object has a published date formatted like "2012-01-11T20:49:59.415Z". If I initialize a Javascript Date object using the code below, the object returns "Invalid Date".

var dt = new Date( "2012-01-11T20:49:59.415Z" );

我在iOS /移动Safari上使用此功能,如果这有所作为。

I'm using this on iOS/mobile Safari, if that makes a difference.

有关如何创建有效对象的任何建议或想法?

Any suggestions or ideas on how to create a valid object?

推荐答案

http://zetafleet.com/blog/javascript-dateparse- for-iso-8601 。看起来日期格式叫做ISO 8601。在较早版本的浏览器(Safari 4,Chrome 4,IE 6-8)中,不支持ISO 8601,因此Date.parse不起作用。从链接的博客文章引用的代码将当前的Date类扩展为支持ISO 8601。

I ended up finding a solution at http://zetafleet.com/blog/javascript-dateparse-for-iso-8601. It looks like the date is in a format called 'ISO 8601.' On earlier browsers (Safari 4, Chrome 4, IE 6-8), ISO 8601 is not supported, so Date.parse doesn't work. The code referenced from the linked blog post extends the current Date class to support ISO 8601.

这篇关于在Javascript中解析Youtube API日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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