I receive from the server a string with a date that looks like this: "2013-03-29T15:27:00Z". How can I format it and transform it into an NSDate?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Try this:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:@"2013-03-29T15:27:00Z"];
回答2:
NSString *departTimeDate = @"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
NSLog(@"Expected Result___ %@",date);
回答3:
NSString *string=@"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:string];