Return true if it is a valid US short date format(MMddyyyy). - CSharp System

CSharp examples for System:DateTime Format

Description

Return true if it is a valid US short date format(MMddyyyy).

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using System;/*from   w w w . j av a  2  s. c om*/

public class Main{
        /// <summary>
        /// Return true if it is a valid US short date format(MM/dd/yyyy).
        /// This regex will match SQL Server datetime values, allowing date only,
        /// allowing zero padded digits in month, day and hour,
        /// and will match leap years from 1901 up until 2099.
        /// </summary>
        /// <param name="sender"></param>
        /// <returns>True if valid otherwise false</returns>
        /// <seealso>http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5</seealso>
        /// <remarks>
        /// <b>Since</b>   2009-05-27<br/>
        /// </remarks>
        public static bool IsDateTimeWithMonthDayYear(string sender)
        {
            if (sender == null) return false;
            if (sender.Trim().Length == 0) return false;
            //Regex regex = new Regex("^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$");
            Regex regex = new Regex("^((((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9]))))[\\-\\/\\s]?\\d{2}(([02468][048])|([13579][26])))|(((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))[\\-\\/\\s]?\\d{2}(([02468][1235679])|([13579][01345789]))))(\\s(((0?[1-9])|(1[0-2]))\\:([0-5][0-9])((\\s)|(\\:([0-5][0-9])\\s))([AM|PM|am|pm]{2,2})))?$");
            return regex.IsMatch(sender);
        }
}

Related Tutorials