The input string is '20100908041312'
the format is year,month,day,Hours,minutes,seconds,time zone
and I have ben trying to convert it to an NSDate with this: @"yyyyMMddHHmmsszz"
But NSDate is nil, anyone see what I'm doing wrong?
-(NSDate*)convertPDFDateStringAsDate:(NSString*) _string{
//20100908041312
//year month day Hours minutes seconds and time zone
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMddHHmmsszz"];
NSDate *date = [dateFormat dateFromString:_string];
//date is nil...
[dateFormat release];
return date;
}
EDIT: its the time zone breaking it
EDIT2: @"yyyyMMddHHmmssTZD"
stops it returning nil, but dosnt pick the time zone correctly
EDIT3: This was the code I Used in the end...i found that the format changes from PDF to PDF so the code deals with the variations that i Found, in some cases this wont extract the Time Zone Properly.
-(NSDate*)convertPDFDateStringAsDate:(NSString*) _string{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString * formatCheck = [_string substringToIndex:2];
if(![formatCheck isEqualToString:@"D:"])
{
NSLog(@"ERROR: Date String wasnt in expected format");
return nil;//return [NSDate date];
}
NSString * extract = [_string substringFromIndex:2];
//NSLog(@"DATESTRING:'%@'",extract);NSLog(@"DATELENGTH:%i",[extract length]);
if([extract length]>14)
{
[dateFormat setDateFormat:@"yyyyMMddHHmmssTZD"];
}
else
{
[dateFormat setDateFormat:@"yyyyMMddHHmmss"];
}
NSDate * date = [dateFormat dateFromString:extract];
[dateFormat release];
return date ;
}