且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS中将日期字符串解析为NSDate对象?

更新时间:2023-01-16 10:50:22

你不要我们需要尽可能多的单引号(仅限非日期/时间字符),所以请更改:

You don't need near as many single quotes as you have (only needed on non date/time characters), so change this:

[self.dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

对此:

[self.dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
...
self.currentQuestion.updated = [self.dateFormatter dateFromString:[self.currentParsedCharacterData stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange([self.currentParsedCharacterData length] – 5,5)]];






此处的文档: https://开发人员.apple.com / library / content / documentation / Cocoa / Conceptual / DataFormatting / Articles / dfDateFormatting10_4.html#// apple_ref / doc / uid / TP40002369-SW1

Unicode格式模式: http://unicode.org/reports/tr35 /tr35-6.html#Date_Format_Patterns

Unicode Format Patterns: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

处理带冒号的时区(+00:00): http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/

Dealing with TimeZones with Colons (+00:00): http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/