NSDateFormatter解析带或不带毫秒的ISO8601 [英] NSDateFormatter to parse ISO8601 with and without milliseconds

查看:118
本文介绍了NSDateFormatter解析带或不带毫秒的ISO8601的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 NSDateFormatter 解析ISO8601日期(带和不带毫秒)。

Is it possible to have a NSDateFormatter parse both an ISO8601 date with and without milliseconds.

API返回以下2种日期格式,有时包括毫秒,有时不包括。

I have the following 2 date formats being returned by an API and sometimes they have milliseconds included and sometimes not.

例如,我可能有 2013-07- 23T13:45:02Z 2013-07-23T13:45:02.677Z 。对于前者,将 dateFormat 属性设置为 @ yyyyMMdd'T'HHmmss'Z' 可以正常工作。

For example I might have 2013-07-23T13:45:02Z and 2013-07-23T13:45:02.677Z. For the former setting the dateFormat property to @"yyyyMMdd'T'HHmmss'Z'" works fine.

是否可以使用一种格式来解析后面的格式,如果存在的话,也可以选择解析毫秒数?

Is there a format I can use to parse the later, optionally parsing the milliseconds if they exist?

推荐答案

用于 2013-07-23T13:45:02Z

使用: yyyy-MM-dd'T'HH:mm:ssZ

For "2013-07-23T13:45:02Z"
Use: "yyyy-MM-dd'T'HH:mm:ssZ"

对于 2013-07-23T13:45:02.677Z

使用: yyyy-MM-dd'T'HH:mm:ss.SSSZ

For "2013-07-23T13:45:02.677Z"
Use: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

检查日期格式字符串以查看使用哪个格式,或尝试使用其中一个,如果失败,请尝试另一个格式。

Check the date format string to see which to use or try one and if it fails try the other.

请参见: ICU格式化日期和时间

这篇关于NSDateFormatter解析带或不带毫秒的ISO8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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