Date Format - time zone

2019-07-24 09:19发布

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?

3条回答
孤傲高冷的网名
2楼-- · 2019-07-24 09:21
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];
查看更多
迷人小祖宗
3楼-- · 2019-07-24 09:29
    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);
查看更多
唯我独甜
4楼-- · 2019-07-24 09:44

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"];
查看更多
登录 后发表回答