I am using the following code to convert an NSString to an NSDate:
+(NSDate *)dateOfDateTimeString:(NSString *)string {
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy.MM.dd_HH:mm aa"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(@"try to convert string: %@ to date: %@", string, date);
return date;
}
However I am getting unexpected results:
2012-06-14 22:26:59.208 Motilink[2223:707] +[NSDate(Util) dateOfDateTimeString:][Line 117] try to convert string:
2012.06.12_21:26 PM to date: 2012-06-12 03:26:00 +0000
2012-06-14 22:26:59.226 Motilink[2223:707] +[NSDate(Util) dateOfDateTimeString:][Line 117] try to convert string:
2012.06.13_15:00 PM to date: 2012-06-13 03:00:00 +0000
What am I doing wrong?
There is no 21:26 PM. Either go for 9:26 PM or skip the “aa” part in your format.