C # method for judging the correct date format entered by regular expressions,

Source: Internet
Author: User

C # method for judging the correct date format entered by regular expressions,
This article will introduce an example code to explain how to use regular expressions to make C # determine whether the input date format is correct. I hope this code will help you.

Generally, when we use C # To Write System programs or Web development, we will encounter a situation where we need to verify whether the input string is a date, the following describes a very comprehensive method to verify a date using a regular expression:

C Regular Expression Date Code 1:

///

/// Whether it is a date string

///

///

Date string)

///

Public static bool IsDate (string StrSource)

{

Return Regex. IsMatch (StrSource, @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-9]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ ");

}

///

/// Whether it is a time string

///

///

Time string (15:00:00)

///

Public static bool IsTime (string StrSource)

{

Return Regex. IsMatch (StrSource, @ "^ (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D: [0-5]? \ D) $ ");

}

///

/// Whether it is a date + time string

///

///

///

Public static bool IsDateTime (string StrSource)

{

Return Regex. IsMatch (StrSource, @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D: [0-5]? \ D) $ ");

}

C Regular Expression Date Code 2:

// Whether it is an integer

Public static bool IsInt (string StrSource)

{

Return Regex. IsMatch (StrSource, @ "^ [0-9] * $ ");

}

Public static bool IsDate (string strDate)

{

If (string. IsNullOrEmpty (strDate ))

Return false;

String s_reg = @ "^ (? Ni :(? = \ D )((? 'Year' (1 [6-9]) | ([2-9] \ d )(? 'Sept' [/.-]) (? 'Month' 0? [1-9] | 1 [012]) \ 2

(? 'Day '((? (0? [1-9] | 1 [012]) (: [0-5]? \ D) {0, 2} (\ x20 [AP] M) | ([01]?

\ D | 2 [0-3]) (: [0-5]? \ D) {1, 2 }))?) $ ";

Regex reg = new Regex (s_reg );

If (reg. IsMatch (strDate. ToLower ()))

Return true;

Else

Return false;

}

The above section describes how to use the C # regular expression to determine whether the input date format is correct. It is helpful for you to learn the c Regular Expression date judgment. At the same time, I would like to thank you for your constant support for our website.

Related Article

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.