DateTime.ParseExact strings to datetime

I'm about to begin cursing at my computer!

I have one program that output a datetime as a string, but I want to feed it into another as a datetime.

The string I get is on the form:

dd/MM/yy hh:mm:ss

And I would like to find an appropriate way to get a DateTime object back.

I'm thinking something like:

string date = "11/07/14 18:19:20";

string dateformat = "dd/MM/yy hh:mm:ss";
DateTime converted_date = DateTime.ParseExact(date, 
         dateformat, CultureInfo.InvariantCulture);

But several of the conversion of dates result in an Exception being thrown back with the message "Not valid timedate".

What am I missing?

Upvotes: 0

Views: 55

Answers (2)

Nicholas Hornback
Nicholas Hornback

Reputation: 126

The hour is not in 12-hour format. For 24-hour format, it's H.

            string date = "11/07/14 18:19:20";

        string dateformat = "dd/MM/yy H:mm:ss";
        DateTime converted_date = DateTime.ParseExact(date,
                 dateformat, CultureInfo.InvariantCulture);

Upvotes: 2

Jonathan
Jonathan

Reputation: 5018

'hh' for hour is actually 12 hour clock, 01-12. I think you want 'HH' or 'H' for 24-hour clock ('HH' is zero-padded, 'H' is not). Check out: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx for specific formats.

Upvotes: 3

Related Questions