WPF C# How to validate textbox input as Date format

c# datetime entity-framework-6 wpf

Question

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.

1
3
4/10/2017 5:47:51 AM

Accepted Answer

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);
2
4/9/2017 11:00:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow