Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
910 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...