-1

Using iText7 and documentinfo.GetMoreInfo("ModDate"), I get the following date string:

D:20220817113241+00'00'

How do I parse this in VB.NET?

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • I think you would have to do a tiny bit of post-processing on the offset, and otherwise it should be straightforward to compose a format string to parse this using `DateTime.ParseExact` (there isn't a format string for the minutes offset, and the full offset string appears to expect what I assume to be the locale-based separator). Did you try looking in the docs before asking here? – Craig Aug 23 '22 at 18:26
  • If you tidy up that example string to `"20220817113241+00:00"`, you can use `Dim yourDate = DateTime.ParseExact(s, "yyyyMMddHHmmsszzz", Nothing)` where `s` contains the tidied string. – Andrew Morton Aug 23 '22 at 18:32
  • 1
    @AndrewMorton I think you need ff after the ss as there are hundredths of a second if I'm counting right. – Craig Aug 23 '22 at 18:49
  • Also, this feels like something that should be a duplicate, but I'm not sure there is one that's a great match. Nearest I found with a search for ParseExact was https://stackoverflow.com/questions/70274461/convert-time-and-date-in-string-to-date-vb-net – Craig Aug 23 '22 at 18:54
  • @Craig I copied that straight from a working test - there aren't any fractions of a second - it's 2022-08-17 11:32:41. – Andrew Morton Aug 23 '22 at 19:11
  • 1
    @AndrewMorton i.e. I did not, in fact, count correctly. :-) – Craig Aug 23 '22 at 21:24
  • @KJ What do you mean by "shift 4", please? – tmighty Aug 23 '22 at 22:37
  • @AndrewMorton What do you mean by "tidy up"? I have removed the trailing "D:", but it throws a System.FormatExpection error when I use ""20220821165805+02'00'"". – tmighty Aug 23 '22 at 22:39

1 Answers1

0

With the help of the comments, I found a solution:

Initial date string:

D:20220817113241+00'00'

If string starts with "D:", remove it.

20220817113241+00'00'

If a string has a +, remove the part beginning with the +.

20220817113241

Use a full format string like this:

yyyyMMddHHmmsszzz

Now measure the length of the remaining date string.

For example:

        Dim s As String = "D:20220817113241+00'00'"

        Dim sFormat As String = "yyyyMMddHHmmsszzz"
        Dim iLen As Integer = s.Length

        sFormat = Mid(sFormat, 0, iLen)

        This makes the format string as long as the date string and cuts off anything that is not present in the date string, like "zzz".

Now use that format string like this:

Dim yourDate = DateTime.ParseExact(s, sFormat, Nothing)
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • @KJ Do you perhaps have a link to documentation for a "D: string"? – Andrew Morton Aug 24 '22 at 18:02
  • @KJ Thanks! It looks like the PDF standard should say "PDF defines a *non-standard* date format..."! Especially as it post-dates ISO 8601. Oh well, too late to change it now I suppose. – Andrew Morton Aug 24 '22 at 18:55