This question was asked a lot, but I can't find the solution for my problem.
What I want:
Convert a date given as a NSString into a NSDate with a complete different format.
I want to convert Tue, 31 Jul 2012 10:15:00 GMT
to 2012-07-31 10:15:00 +0000
.
My code:
NSString *startDateString = @"Tue, 31 Jul 2012 10:15:00 GMT"; //in real from a server
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startDateString];
[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss'Z'"];
NSString *endDateString = [formatter stringFromDate:startDate];
NSDate *endDate = [formatter dateFromString:endDateString];
Problem:
endDate
is nil
.
Edit:
Found a strange behaviour.
When I change the first dateFormat to @"EEE, dd MMM yyy HH:mm:ss 'GMT'"
(added GMT) then the output of endDate is correct, but only in the Simulator. On the device it is still nil.
With the dateFormat without "GMT" the value is nil on the Simulator and on the Device.
this is working well for me, you might use it:
the
endDate
is:UPDATE #1
it has been tested on the real device only!
Add the following before converting