how to Regular Expression for date format in C#

 Admin   
614 View    Dec 23 2019 5:06PM

HI,

 

Regular Expression use as below

 

//Verify whether date entered in dd/MM/yyyy format.

Regex regex = new Regex(@"(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$");

 

bool isValid = regex.IsMatch(txtDate.Text.Trim());

 

//Verify whether date entered in MM/dd/yyyy format.

Regex regexDate = new Regex(@"^(0?[1-9]|1[0-2])/(0?[1-9]|1[0-9]|2[0-9]|3[01])/\d{4}$");

 

bool isValidDate = regexDate.IsMatch(txtDate.Text.Trim());

 

 

//Verify whether Time entered in HH:MM format.

Regex regexTime = new Regex(@"^(?:[01]?[0-9]|2[0-3]):[0-5][0-9]$");

 

bool isValidTime = regexTime.IsMatch((txtTime.Text.Trim());