将Adobe PDF日期字符串转换为NSDate [英] Converting Adobe PDF date String to NSDate

查看:110
本文介绍了将Adobe PDF日期字符串转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入字符串为'20100908041312'

The input string is '20100908041312'

格式为年,月,日,小时,分钟,秒,时区

the format is year,month,day,Hours,minutes,seconds,time zone

并且我试图将其转换为NSDate: @yyyyMMddHHmmsszz

and I have ben trying to convert it to an NSDate with this: @"yyyyMMddHHmmsszz"

但NSDate是零,有人看到我做错了吗?

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;
}

编辑:它打破它的时区

EDIT2: @yyyyMMddHHmmssTZD停止返回nil,但dosnt正确选择时区

@"yyyyMMddHHmmssTZD" stops it returning nil, but dosnt pick the time zone correctly

EDIT3:这是我最后使用的代码...我发现格式从PDF变为PDF所以代码处理我发现的变化,在某些情况下这不会正确提取时区。

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 ;
}


推荐答案

我认识到帖子结束了一岁,但没有时间太晚,无法获得更好的答案。

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

由于您指定的输入字符串是Adobe PDF日期字符串,因此格式应符合PDF规范,根据规范: YYYYMMDDHHmmSSOHH'mm (省略前缀D :)。

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:).

注意输入字符串长度为14个字符并且您的NSDate格式长度为16个字符,因此时区不在您输入的字符串中。

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.

尽管如此,您问题的真正答案是使用 Quartz 2D CGPDFString 功能:
 

CFDateRef CGPDFStringCopyDate(CGPDFStringRef string
);

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

该函数返回一个CFDateRef,它具有到NSDate的免费桥接器,因此您可以将从PDF读取的日期传递给此函数,并通过强制转换轻松获取NSDate 。

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.

这篇关于将Adobe PDF日期字符串转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆