Why can't NSDateFormatter convert string correctly

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?

Answers


There is no 21:26 PM. Either go for 9:26 PM or skip the "aa" part in your format.


Need Your Help

Spring data jpa mssql driverClassName error

java spring hibernate spring-data-jpa

I am trying to connect to MSSQL but I am getting the error:

A couple of pattern matching issues with pattern matching in Lua

lua lua-patterns

I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specif...