如何将以"20110912T220000&"(NSString)形式给出的日期转换为NSDate [英] How to convert a date given as "20110912T220000"(NSString) to NSDate

查看:76
本文介绍了如何将以"20110912T220000&"(NSString)形式给出的日期转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以"20110912T220000"格式获取日历事件的开始日期和结束日期(同时解析.ics文件).如何将其转换为NSDate,以添加为事件(EKEvent)的startDate属性.

I am getting start and end dates for my calendar event(while parsing a .ics file) in "20110912T220000" format. How can I convert this to a NSDate to add to add as event(EKEvent)'s startDate property.

如果有人知道,请尽快帮助我.

If anyone knows please help me soon.

推荐答案

为此,您应该使用NSDateFormatter.

请参见数据格式指南 (日期和时间编程指南可能也很有趣)

See Data Formatting Guide (the Date & Time Programming Guide may also be interesting)

这在本技术说明中也有详细说明苹果的问与答. 请注意,在这种情况下,应使用特殊的"en_US_POSIX"语言环境,如本技术说明中所述.

This is also detailed in this Technical Note in Apple's Q&As. Note that for such situations, you should use the special "en_US_POSIX" locale as explained in this technical note.

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[df setDateFormat:@"yyyyMMdd'T'HHmmss"];
NSDate* parsedDate = [df dateFromString:...];

这篇关于如何将以"20110912T220000&"(NSString)形式给出的日期转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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