Method for verifying the correctness of the date

xiaoxiao2021-03-06  139

If you want to check the date you entered on the web application, often

It will be necessary to deal with different inputs, many of which are illegal, and provide a good test method below:

Static Bool Validatedate (String Date, String Format)

{

Try

{

System.globalization.datetimeformatinfo dtfi = new system.globalization.datetimeformatinfo ();

DTFI.ShortdatePattern = Format;

DateTime DT = DateTime.Parsexact (Date, "D", DTFI);

}

Catch (Exception)

{

Return False;

}

Return True;

}

BOOL SUCCESS;

Success = Validatedate ("3403", "MMMM"); // False As 34 IS Not a Valid Month

Success = Validatedate ("3403", "YYMM"); // true

Success = Validatedate ("1212", "MMDD"); // True

转载请注明原文地址:https://www.9cbs.com/read-127385.html

New Post(0)