Used to verify the validity of the date, the user entered the irregular date also made simple processing, such as the user entered "Today", the code will think that the user is to return today's date, in addition to the date of the pure number can be resolved, such as: 20130906
/// <summary>///Verify that the date is legal and that the irregular process is simple/// </summary>/// <param name= "Date" >Date</param> Public Static BOOLIsDate (ref stringdate) {//if empty, the validation is considered to be qualifiedif(IsNullOrEmpty (date)) {return true;}//clear spaces in the string to validateDate =date. Trim ();//replace \Date = date. Replace (@"\","-");//Replace/Date = date. Replace (@"/","-");//If you find the kanji "Today", it is the current dateif(date. IndexOf ("Today") != -1) {Date=DateTime.Now.ToString ();}Try{//test whether a date character is a rule with a transformDate = Convert.todatetime (date). ToString ("D");return true;}Catch{//returns False if a non-number exists in the date stringif(!isint (date)) {return false;}#regionTo parse a pure number//parsing of 8-bit pure numbersif(date. Length = =8){//Get Month DaystringYear = date. Substring (0,4);stringmonth = date. Substring (4,2);stringDay = date. Substring (6,2);//validation Legitimacyif(Convert.ToInt32 (year) <1900|| Convert.ToInt32 (year) >2100){return false;}if(Convert.ToInt32 (month) > A|| Convert.ToInt32 (Day) > to){return false;}//Stitching DateDate = Convert.todatetime (year +"-"+ Month +"-"+ day). ToString ("D");return true;}//parsing of 6-bit pure numbersif(date. Length = =6){//Get DatestringYear = date. Substring (0,4);stringmonth = date. Substring (4,2);//validation Legitimacyif(Convert.ToInt32 (year) <1900|| Convert.ToInt32 (year) >2100){return false;}if(Convert.ToInt32 (month) > A){return false;}//Stitching DateDate = Convert.todatetime (year +"-"+ month). ToString ("D");return true;}//parsing of 5-bit pure numbersif(date. Length = =5){//Get DatestringYear = date. Substring (0,4);stringmonth = date. Substring (4,1);//validation Legitimacyif(Convert.ToInt32 (year) <1900|| Convert.ToInt32 (year) >2100){return false;}//Stitching DateDate = year +"-"+month;return true;}//parsing of 4-bit pure numbersif(date. Length = =4){//Get yearstringYear = date. Substring (0,4);//validation Legitimacyif(Convert.ToInt32 (year) <1900|| Convert.ToInt32 (year) >2100){return false;}//Stitching DateDate = Convert.todatetime (year). ToString ("D");return true;}#endregionreturn false;}}
C # verifies that a date of a given string is legal