C # verifies that a date of a given string is legal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.