Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime TryParse problem

Tags:

c#

string date = txtWorkingDate.Text;
            DateTime dateTime = DateTime.MinValue;

            if (DateTime.TryParse(date, out dateTime))
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;

txtWorkingDate.Text is like "dd.MM.yyyy" becouse of this validateion is always false if date is not like "dd.MM.yyyy". How c an i check types of date like "dd.MM.yyyy", "MM/dd/yyyy" becouse are all valid.

like image 741
senzacionale Avatar asked Nov 29 '22 17:11

senzacionale


1 Answers

By using this overload and providing the accepted formats:

string date = txtWorkingDate.Text;
DateTime dateTime;
string[] formats = new[] { "dd.MM.yyyy", "MM/dd/yyyy" };
if (DateTime.TryParseExact(date, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
    args.IsValid = true;
}
else
{
    args.IsValid = false;
}
like image 90
Darin Dimitrov Avatar answered Dec 02 '22 08:12

Darin Dimitrov