1
votes

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 ;
}
3
D'oh! I did all that research for nothing! :)Matthew Frederick

3 Answers

2
votes

It should follow the unicode standard for the setDateFormat: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

You should be able to set the timezone this way:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
//Optionally for time zone conversations
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

Alex

1
votes

"12" isn't a valid time zone for any of the Unicode date format patterns, so NSDateFormatter returns nothing. http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

You may have to use all but the last two digits, then set the time zone on your new NSDate object by translating Adobe's two-digit number to an appropriate time zone.

1
votes

I recognize the posting is over a year old, but no time is too late for a better answer.

Since you specify the input string is an Adobe PDF date string, then the format should conform to the PDF specification, which per the spec is: YYYYMMDDHHmmSSOHH'mm (omitting the prefix D:).

Note your input string is 14 characters long and your NSDate format is 16 characters long, so the timezone isn't in your input string as stated.

Nonetheless, the real answer to your question is to use the Quartz 2D CGPDFString function:
 
CFDateRef CGPDFStringCopyDate (CGPDFStringRef string );

The function returns a CFDateRef which has a toll-free bridge to NSDate, so you can pass the date read from a PDF to this function and easily get back an NSDate by casting.