Andra Todorescu
Andra Todorescu

Reputation: 628

Date Format - time zone

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?

Upvotes: 2

Views: 2663

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

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

Upvotes: 1

βhargavḯ
βhargavḯ

Reputation: 9836

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

Upvotes: 1

Leo Chapiro
Leo Chapiro

Reputation: 13979

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

Upvotes: 5

Related Questions