Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string is a valid date using DateTime.TryParse

Tags:

c#

.net

I am using DateTime.TryParse() function to check if a particular string is a valid datetime not depending on any cultures.
To my surprise , the function returns true for even strings like "1-1", "1/1" .etc.

How can I solve this problem?

Update:

Does it mean, if I want to check if a particular string is valid datetime, I need a huge array of formats?? There will be different combinations , I believe.
Even there are lots of date separator ( '.' , '/' , '-', etc..) depending on the culture, it will be difficult for me to define an array of format to check against .
Basically, I want to check if a particular string contains AT LEAST day(1 through 31 or 01 through 31),month(1 through 12 or 01 through 12) and year(yyyy or yy) in any order, with any date separator , what will be the solution?
So, if the value includes any parts of time, it should return true too. I could NOT be able to define a array of format.

like image 637
Kai Avatar asked Apr 18 '13 05:04

Kai


Video Answer


1 Answers

If you want your dates to conform a particular format or formats then use DateTime.TryParseExact otherwise that is the default behaviour of DateTime.TryParse

DateTime.TryParse

This method tries to ignore unrecognized data, if possible, and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes the time is 12:00 midnight. If s includes a date component with a two-digit year, it is converted to a year in the current culture's current calendar based on the value of the Calendar.TwoDigitYearMax property. Any leading, inner, or trailing white space character in s is ignored.

If you want to confirm against multiple formats then look at DateTime.TryParseExact Method (String, String[], IFormatProvider, DateTimeStyles, DateTime) overload. Example from the same link:

string[] 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"}; string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",                          "5/1/2009 6:32:00", "05/01/2009 06:32",                          "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"};  DateTime dateValue;  foreach (string dateString in dateStrings) {    if (DateTime.TryParseExact(dateString, formats,                                new CultureInfo("en-US"),                                DateTimeStyles.None,                                out dateValue))       Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);    else       Console.WriteLine("Unable to convert '{0}' to a date.", dateString); } // The example displays the following output:  //       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.  //       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.  //       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.  //       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.  //       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.  //       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM. 
like image 136
Habib Avatar answered Oct 08 '22 19:10

Habib