Http Date at NSDate

I have a line taken from the header field of the HTTP response "Date" in this format:

"Sun, 24 Jun 2012 16:34:51 GMT" 

I want to convert this string to an NSDate object. For this area, I created an instance of NSDateFormatter using a different format:

 [dateFormatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"]; [dateFormatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss 'GMT'"]; 

but when I print a string from a date using:

 [dateFormatter dateFromString:date] 

I get:

 (null) 

where am i doing wrong?

+7
source share
1 answer

Not sure if I understand the problem, since your own code seems to work fine for me:

 NSString *dateStr = @"Sun, 24 Jun 2012 16:34:51 GMT"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"]; NSDate *date = [dateFormatter dateFromString:dateStr]; NSLog(@"Date: %@", date); 

What gives this conclusion:

Date: 2012-06-24 16:34:51 +0000

If I do not understand your question, this is the correct result.

+8
source

All Articles