-1

I have issue to convert ISO date time to local datetime.

the flowing code is not work with me and give error

Dim txt As String = "20200530T015253+08"
Dim output As DateTime = DateTime.ParseExact(txt, "u", System.Globalization.CultureInfo.InvariantCulture)

the error is

System.FormatException: 'String was not recognized as a valid DateTime.'

stuartd
  • 70,509
  • 14
  • 132
  • 163
digitala
  • 15
  • 3
  • Take a look at the answers in [this thread](https://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format). Seems .NET's DateTime parsing may not handle all possible formulations of ISO 8601. – Sean Skelly May 30 '20 at 00:08
  • `Dim output = DateTimeOffset.ParseExact("20200530T015253+08".Replace("+", " +"), "yyyyMMddTHHmmss zz", Nothing, DateTimeStyles.AssumeUniversal)` – Jimi May 30 '20 at 00:12
  • 1
    It looks like UTC offsets are written like "+08:00", not "+08" (which makes sense if you consider places like India and Newfoundland). At least here they are: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings. I think you will need to specify the whole format exactly as @Jimi has suggested – Flydog57 May 30 '20 at 00:14

1 Answers1

0

Your date string is not an "ISO date" as far as I can see. ISO 8601 would lay it out like "2020-05-30T01:52:53+08:00" and .net would parse it with "o"

Parse it by specifying the format:

DateTime.ParseExact("20200530T015253+08", "yyyyMMddTHHmmsszz", Nothing)

If your input data will vary its presentation for time zones that have a fractional hours component such as India being 5.5 hours, then you'll need to vary your format string to zzz

Caius Jard
  • 72,509
  • 5
  • 49
  • 80