DateTime.ParseExact - date and time in the UK

I am trying to parse the following British DateTimestring format :24/01/2013 22:00

However, I keep getting this error:

The string was not recognized as a valid DateTime.

CultureInfo.CurrentCulturereturns " en-GB", which is correct

Here is my code

    [TestMethod]
    public void TestDateTimeParse()
    {
        DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "d/M/yyyy hh:mm", CultureInfo.CurrentCulture);

        int hours = tester.Hour;
        int minutes = tester.Minute;

        Assert.IsTrue(true);
    }
+5
source share
3 answers

hh- for 12 hours. You should use instead hh.

DateTime.ParseExact("24/01/2013 22:00", 
                    "d/M/yyyy HH:mm", // <-- here
                    CultureInfo.CurrentCulture)
+15
source

"hh" for an hour using a 12-hour clock from 01 to 12

"hh" for an hour using a 24-hour clock from 00 to 23

Try with this:

public static void Main(string[] args)
{
    DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
}

Here DEMO.

You can also check Custom Date and Time Format Stringsfrom MSDN.

+3
source

Your format is incorrect, try the following:

DateTime tester = DateTime.ParseExact("24/01/2013 22:00", "dd/MM/yyyy HH:mm", CultureInfo.CurrentCulture);
+1
source

All Articles