views:

867

answers:

2

Hi,

Quick question: I'm trying to parse dates in the following format to their equivalent DateTime data type, but for some reason it's failing.

The string format is: 28/May/2009:17:43:04 +0000

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

Here's 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, that produces a FormatException.

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

Any pointers would be greatly appreciated; it's Friday and my brain has gone to sleep! :)

Thanks!

+10  A: 

You want "HH" (24 hour format) rather than "hh" (12 hour format) in your format string:

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);
    }    
}
Jon Skeet
Arrrgh! Apparently I went FormatBlind. :) Thanks for pointing out my riduclously obvious mistake and restoring my former, happy, Friday feeling!
Richard
+7  A: 

hh is hours in the 12-hour clock. You want HH.

AakashM