Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net: How to parse a date with this format: 08 Feb 2011 06:46

Tags:

.net

datetime

How do I use DateTime.Parse to parse a datetime with the following format:

08 Feb 2011 06:46

In response to the answer I've received so far, I tried the following:

item.ServerDate = DateTime.ParseExact
                ("08 Feb 2011 06:46", "dd MMM yyyy hh:mm", System.Globalization.CultureInfo.InvariantCulture);

I still get the exception: String was not recognized as a valid DateTime.

UPDATE: The following code works without the hour and minute:

DateTime.ParseExact("08 Feb 2011","dd MMM yyyy",null)
like image 599
Vivian River Avatar asked Mar 16 '11 19:03

Vivian River


People also ask

What is DateTime parse in C#?

The Parse method tries to convert the string representation of a date and time value to its DateTime equivalent. It tries to parse the input string completely without throwing a FormatException exception.

What formats does DateTime parse?

Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.sssZ ) is explicitly specified to be supported.

How do I format a date string?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.

What does it mean to parse dates?

Parsing dates allows a user to format a Date input from a Form to the local standard format. The Parse Date with Format step converts a String into a DateTime data format. The Get Formatted Date step will convert a DateTime data input into a String.


2 Answers

DateTime.ParseExact("08 Feb 2011 06:46", "dd MMM yyyy hh:mm", 
    System.Globalization.CultureInfo.InvariantCulture);

In your question's sample code, you forgot to capitalize all of the month "M"s.

Edit

As Anton points out, the "H"s also need to be capitalized to use military time.

DateTime.ParseExact("08 Feb 2011 13:46", "dd MMM yyyy HH:mm", 
    System.Globalization.CultureInfo.InvariantCulture)

The above code works for me. I can't imagine why you'd get an error on the same code, when we're specifying the culture. Can you double-check your code and inputs?

like image 128
StriplingWarrior Avatar answered Sep 25 '22 01:09

StriplingWarrior


DateTime.ParseExact might work for you: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

like image 29
Andy White Avatar answered Sep 22 '22 01:09

Andy White