简体   繁体   中英

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)

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

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