Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C# How to validate textbox input as Date format

I have some textboxes where the user enters a date or a time. When I save on database I create

string input = txtdocumentiDate.Text +" "+ txtdocumentiTime.Text;
        ts.Documenti = DateTime.ParseExact(input, "dd/MM/yyyy HH.mm", CultureInfo.InvariantCulture);

(ts is my entity framework database table)

I would want to make a check to see if all the formats of my textboxes are valid before fire button save.

like image 946
Disaji Avatar asked Aug 30 '25 18:08

Disaji


1 Answers

Use DateTime.TryParse. It returns a bool value indicating whether the method was able to parse the string or not.

string input = txtdocumentiDate.Text +" "+ txtdocumentiTime.Text;
DateTime dummy;
if(DateTime.TryParse(input, dummy))
    ts.Documenti = DateTime.ParseExact(input, "dd/MM/yyyy HH.mm", CultureInfo.InvariantCulture);
like image 53
John Stritenberger Avatar answered Sep 02 '25 08:09

John Stritenberger