DateTime.ParseExact - Crash

Quick question: I am trying to parse dates in the following format into their equivalent DateTime data type, but for some reason it does not work.

Line format: 28 / May / 2009: 17: 43: 04 +0000

Or: dd / MMM / yyyy: hh: mm: ss zz00

Here is the code I'm using:

Dim provider As New CultureInfo("en-US")
Dim d As DateTime = DateTime.ParseExact(value, "dd/MMM/yyyy:hh:mm:ss zz00", provider)

But this throws a FormatException.

FYI: I also tried using the InvariantCulture parameter for the ParseExact provider parameter, but to no avail.

Any pointers would be greatly appreciated; it's friday and my brain fell asleep! :)

Thanks!

+3
source share
2 answers

In the format bar, you want “HH” (24-hour format), not “hh” (12-hour format):

using System;
using System.Globalization;

public class Test
{
    static void Main()
    {
        var provider = new CultureInfo("en-US");
        // Doesn't throw
        var d = DateTime.ParseExact("28/May/2009:17:43:04 +0000", 
                                    "dd/MMM/yyyy:HH:mm:ss zz00",
                                    provider);
    }    
}
+15

hh - 12- . hh.

+8

Source: https://habr.com/ru/post/1710341/


All Articles