Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse PDF creation and modified date #949

Open
Sicos1977 opened this issue Dec 2, 2024 · 1 comment
Open

Parse PDF creation and modified date #949

Sicos1977 opened this issue Dec 2, 2024 · 1 comment

Comments

@Sicos1977
Copy link

Sicos1977 commented Dec 2, 2024

For everyone who is wondering how you need to parse these dates to a DateTime value.

#region CreateDateTime
/// <summary>
///     Parses the string to a datetime value or null
/// </summary>
/// <param name="dateTimeString"></param>
/// <returns></returns>
private static DateTime? CreateDateTime(string dateTimeString)
{
    try
    {
        if (dateTimeString.StartsWith("D:"))
            dateTimeString = dateTimeString.Substring(2);

        var month = 1;
        var day = 1;
        var hour = 0;
        var minute = 0;
        var second = 0;
        int offsetHour = 0, offsetMinute = 0;

        var year = int.Parse(dateTimeString.Substring(0, 4));

        if (dateTimeString.Length >= 6)
        {
            month = int.Parse(dateTimeString.Substring(4, 2));
            if (dateTimeString.Length >= 8)
            {
                day = int.Parse(dateTimeString.Substring(6, 2));
                if (dateTimeString.Length >= 10)
                {
                    hour = int.Parse(dateTimeString.Substring(8, 2));
                    if (dateTimeString.Length >= 12)
                    {
                        minute = int.Parse(dateTimeString.Substring(10, 2));
                        if (dateTimeString.Length >= 14)
                            second = int.Parse(dateTimeString.Substring(12, 2));
                    }
                }
            }
        }

        var dateTime = new DateTime(year, month, day, hour, minute, second);

        if (dateTimeString.Length <= 14)
            return dateTime;

        var variation = dateTimeString[14];

        if (variation == 'Z')
            return dateTime.ToLocalTime();

        if (dateTimeString.Length >= 17)
        {
            offsetHour = int.Parse(dateTimeString.Substring(15, 2));
            if (dateTimeString.Length >= 20)
                offsetMinute = int.Parse(dateTimeString.Substring(18, 2));
        }

        var timeSpan = new TimeSpan(offsetHour, offsetMinute, 0);
        if (variation == '-')
            dateTime += timeSpan;
        else
            dateTime -= timeSpan;

        return dateTime.ToLocalTime();
    }
    catch (Exception exception)
    {
        return null;
    }
}
#endregion
@BobLd
Copy link
Collaborator

BobLd commented Dec 8, 2024

@Sicos1977 thanks a lot for that. PdfPig already has UglyToad.PdfPig.Util.DateFormatHelper.TryParseDateTimeOffset(...) - it might be worth merging your efforts with what's available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants