TAGS :Viewed: 3 - Published at: a few seconds ago

[ converting a string to date in the exact manner ]

I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).

I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:

1.DateTime date = DateTime.Parse("11 Jan 2011");

2.DateTime date = DateTime.ParseExact("11 Jan 2011" , @"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);

Answer 1

parsing and displaying are not the same thing

you parse the original string to a DateTime object but display results using Date/Time format strings

Answer 2

Both your calls are correct.

A DateTime structure preserves no information about formatting; it just represents the raw date and time.

What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");