我正在尝试解析RSS提要,但是日期很难解析。日期字符串为:publishedDate = "2013-01-08T20:09:02.000Z"
下面是我的代码:
NSDateFormatter *utc = [[NSDateFormatter alloc] init];
[utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *dtUTC = [utc dateFromString: publishedDate];
NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:@"yyyy-MM-dd"];
[dfLocal setTimeZone:[NSTimeZone localTimeZone]];
NSString *time =[dfLocal stringFromDate:dtUTC];
NSLog(@"original UTC %@ now local: %@", dtUTC, time);为什么date返回nil?另外,我正在尝试将UTC时间转换为CST时间。
发布于 2013-01-09 10:57:23
使用
[utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.'000Z'"];发布于 2013-01-09 11:03:26
对于包含毫秒的stime,使用"S“。
[utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'"];
NSString *publishedDate = @"2013-01-08T20:09:02.000Z";
NSDateFormatter *utc = [[NSDateFormatter alloc] init];
[utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'"];
NSDate *dtUTC = [utc dateFromString: publishedDate];
NSLog(@"dtUTC: %@", dtUTC);NSLog输出:
dtUTC: 2013-01-08 20:09:02 +0000
SDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:@"yyyy-MM-dd"];
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *time =[dfLocal stringFromDate:dtUTC];
NSLog(@"original UTC %@ now local: %@", dtUTC, time);NSLog输出:
原始UTC 2013-02-08 20:09:02 +0000现在本地: 2013-02-08
发布于 2013-01-09 11:09:09
只需将您的时区名称更改为CST,并按如下方式更改setDateFormat:
NSDateFormatter *cst = [[NSDateFormatter alloc] init];
[cst setTimeZone:[NSTimeZone timeZoneWithName:@"CST"]];
[cst setDateFormat:@"yyyy-mm-dd'T'HH:mm:ss.'000Z'"];
NSDate *theCST = [cst dateFromString:publishedDate];
NSLog(@"theCST is %@", theCST);https://stackoverflow.com/questions/14227585
复制相似问题