日期格式 - 时区

时间:2013-04-10 10:11:50

标签: ios objective-c nsdate nsdateformatter

我从服务器收到一个字符串,其日期如下:“2013-03-29T15:27:00Z”。如何格式化并将其转换为NSDate?

3 个答案:

答案 0 :(得分:5)

试试这个:

[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"];

答案 1 :(得分:1)

    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);

答案 2 :(得分:1)

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];