datetime parse work exactly when date format is like mm/dd/yyyy but when dateformat is like dd/mm/yyyy then problem occur. i want to write code to parse date whatever format is used.
suppose if my date is like “15/01/2012” or 05/12/2012 then my code can parse it as valid datetime. so what should i do… i use code like
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string xxx = DateTime.ParseExact("15/12/2012", "yyyyMMdd", culture).ToString();
it does not work because dateformat is dd/mm/yyyy. so tell me the best solution that when date format would be dd/mm/yyyy or mm/dd/yyyy etc but code can parse it properly. so guide me what code i should use. thanks
You must not allow both MM/dd/yyyy and dd/MM/yyyy formats without any indication of which is used, or you’re just crying out for bad data. What would you interpret “06/05/2011” as? May 6th or June 5th?
You should either specify the format explicitly or you should use whatever the cultural default is (which shouldn’t allow both formats) – but to treat “13/12/2011” differently from “11/12/2011” just because the day value happens to be 13 is a really bad idea, IMO.
(The reason your sample code doesn’t work is that you’re explicitly saying you want it parsed as
yyyyMMdd, which is clearly not the format of"15/12/2012". I’m not sure why you’d ever expect that to work.)