简体   繁体   中英

Error while trying to parse date

I'm using itextsharp to extract the modified date from a pdf file. The string gets returned in this format: D:20120224093340 . I'm trying to parse that string into a date time like so:

string modDate = "";

reader.Info.TryGetValue("ModDate", out modDate);

System.Globalization.CultureInfo provider = 
System.Globalization.CultureInfo.InvariantCulture;

pdfModDate = DateTime.ParseExact(formattedDate, "D:yyyyddMMHHmmss", provider);

But I get this error message: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

I have no idea what this error means. This code should be working...

Your month is 24, which is not supported by the Gregorian calendar :-)

D:20120224093340
D:yyyyddMMHHmmss
        ^^

Try "D:yyyyMMddHHmmss" . You got day and month switched.

The date you provided is in the following format:

D:yyyyMMddHHmmss

Not the english format MM/dd/yyyy

You could try stripping off the D: from the beginning of the string. The ParseExact function recognizes capital D as a special character in the date format string. According to MSDN "D" is short for long date format.

Also, you need to switch day and month. The order they are returning it in is Year, Month, Day, Hour, Minute, Second.

    string date = "D:20120224093340";
        System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
        DateTime dateTime = DateTime.ParseExact(date,"D:yyyyMMddHHmmss",provider);

After switching ddMM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM