简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM