cag*_*glu 3 nsdate nsdateformatter
我2012-08-17T10:56:45.508205从Django API 获取NSString 作为时间.
我试图NSDate用这段代码将该字符串转换为对象:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:stringToConvert];
Run Code Online (Sandbox Code Playgroud)
但是date对象为null.我哪里错了?
您从Django API获得的ISO8601字符串(2012-08-17T10:56:45.508205)不包含时区组件.但是,您在格式字符串中包含该字段符号(Z).只需删除它,它应该工作正常.
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
Run Code Online (Sandbox Code Playgroud)
有关格式代码的详细信息,请参阅Unicode技术参考: