Rodrigo Fuentes
Rodrigo Fuentes

Reputation: 35

DateTime.ParseExact format for custom date

Im unable to use the function parseexact to transform the following String to Date

This one works:

Dim fechaS = "Feb 16 23:13:53.241 2015"
fecha1 = DateTime.ParseExact(fechaS, "MMM dd HH:mm:ss.fff yyyy", Nothing)

But fails, when I add a couple more options:

Dim fechaS = "Mon 16 23:13:53.241 UTC 2015"
fecha1 = DateTime.ParseExact(fechaS, "ddd MMM dd HH:mm:ss.fff z yyyy", Nothing)

Upvotes: 1

Views: 498

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27322

Your second example has three issues with it:

  1. The datetime string is not the same format as the date
  2. The date string has no month in it so this can't be parsed into a date without knowing this.
  3. There is no valid datetime specifier to match "UTC". In order to adjust for timezones you have to supply a timezone offset (this is what the z signifies) . You could to this by replacing UTC with +0

This fails

Dim fechaS = "Mon 16 23:13:53.241 UTC 2015"
fecha1 = DateTime.ParseExact(fechaS, "ddd MMM dd HH:mm:ss.fff z yyyy", Nothing)

This works

Dim fechaS = "Mon Feb 16 23:13:53.241 UTC 2015"
fecha1 = DateTime.ParseExact(fechaS.Replace("UTC", "+0"), "ddd MMM dd HH:mm:ss.fff z yyyy", Nothing)

More info here on the date time formats allowed: Custom DateTime Format Strings

Upvotes: 2

Related Questions