DateTime.TryParse issue with dates of yyyy-dd-MM format DateTime.TryParse issue with dates of yyyy-dd-MM format asp.net asp.net

DateTime.TryParse issue with dates of yyyy-dd-MM format


This should work based on your example "2011-29-01 12:00 am"

DateTime dt;DateTime.TryParseExact(dateTime,                        "yyyy-dd-MM hh:mm tt",                        CultureInfo.InvariantCulture,                        DateTimeStyles.None,                        out dt);


You need to use the ParseExact method. This takes a string as its second argument that specifies the format the datetime is in, for example:

// Parse date and time with custom specifier.dateString = "2011-29-01 12:00 am";format = "yyyy-dd-MM h:mm tt";try{   result = DateTime.ParseExact(dateString, format, provider);   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());}catch (FormatException){   Console.WriteLine("{0} is not in the correct format.", dateString);}

If the user can specify a format in the UI, then you need to translate that to a string you can pass into this method. You can do that by either allowing the user to enter the format string directly - though this means that the conversion is more likely to fail as they will enter an invalid format string - or having a combo box that presents them with the possible choices and you set up the format strings for these choices.

If it's likely that the input will be incorrect (user input for example) it would be better to use TryParseExact rather than use exceptions to handle the error case:

// Parse date and time with custom specifier.dateString = "2011-29-01 12:00 am";format = "yyyy-dd-MM h:mm tt";DateTime result;if (DateTime.TryParseExact(dateString, format, provider, DateTimeStyles.None, out result)){   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());}else{   Console.WriteLine("{0} is not in the correct format.", dateString);}

A better alternative might be to not present the user with a choice of date formats, but use the overload that takes an array of formats:

// A list of possible American date formats - swap M and d for European formatsstring[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",                    "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",                    "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",                    "M/d/yyyy h:mm", "M/d/yyyy h:mm",                    "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",                   "MM/d/yyyy HH:mm:ss.ffffff" };string dateString; // The string the date gets read intotry{    dateValue = DateTime.ParseExact(dateString, formats,                                     new CultureInfo("en-US"),                                     DateTimeStyles.None);    Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);}catch (FormatException){    Console.WriteLine("Unable to convert '{0}' to a date.", dateString);}                                               

If you read the possible formats out of a configuration file or database then you can add to these as you encounter all the different ways people want to enter dates.


Try using safe TryParseExact method

DateTime temp;string   date = "2011-29-01 12:00 am";DateTime.TryParseExact(date, "yyyy-dd-MM hh:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out temp);