locked
regular expression for date

    Question

  • I need a regular expression for date which will have
           (A) mmddyyyy, mm/dd/yyyy, m/d/yyyy are valid inputs
            (B) mdyyyy, mdyy, mm/dd/yy, mm/ddyyyy, mmdd/yyyy are invalid ones
            (C) these validations are enforced as soon as the cursor gets out of focus from the date field
            (D) final displayable format is: mm/dd/yyyy


    Friday, August 31, 2007 12:59 PM

Answers

  • (?x)
    (?:0[1-9]|1[0-2]) #month
    (/?)
    (?:0[1-9]|[12][0-9]|3[01]) #day
    (\1)
    \d{4} #year
    |
    \d/\d/\d{4}

    Regards
    Tuesday, June 01, 2010 3:30 PM
  • Hi,

    that's not really possible in regex (I think), because of the definition of leap years.

    You have to check several things:
    1. is the year divisible by 4 without remainder
    2. is the year divisible by 100 without remainder
    3. is the year divisible by 400 without remainder

    if 1 is true, it might be a leap year (check #2), otherwise it's no leap year
    if 2 is also true, it might be a leap year (check #3), otherwise it is a leap year
    if 3 is also true, it is a leap year, otherwise it's not

    So 1900 was not a leap year but 2000 was one

    1900/4=475
    1900/100=19
    1900/400=4.75

    2000/4=500
    2000/100=20
    2000/400=5.00

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    Tuesday, June 01, 2010 3:24 PM

All replies

  • What have you tried so far?
    Thursday, September 06, 2007 6:07 AM
  • I am able to solve my problem by foloowing regular expression

    (([1-9]|1[012])[- /.]([1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d)|((1[012]|0[1-9])[- /.](3[01]|2\d|1\d|0[1-9])[- /.](19|20)\d\d)

     

     

    For final displayable i have follow the foloowing technique.

     

    if (txtDateOfBirth.Text.Contains("/"))

    {

    DateTime dtime;

    string exactDate = txtDateOfBirth.Text;

    if (exactDate.Length == 8)

    {

    exactDate = exactDate.Insert(0, "0");

    exactDate = exactDate.Insert(3, "0");

    }

    txtDateOfBirth.Text = exactDate;

    }

    else

    {

    DateTime dtime = DateTime.ParseExact(txtDateOfBirth.Text, "mmddyyyy", null);

    txtDateOfBirth.Text = dtime.ToString("mm/dd/yyyy");

    }

    Friday, September 07, 2007 6:32 AM
  • This will match the dates as valid:

     

    [01][0-2][/\.-]?[0-3][0-9][/\.-]\d{4}

     

    Adamus

    Sunday, September 09, 2007 10:26 PM
  • Hi, I need a regular expression for date, validation for leap year, please, thank for you help...

     

    Monday, January 21, 2008 6:06 PM
  • [01][0-2][/\.-]?[0-3][0-9][/\.-]\d{4}

    This is validating the date 01/33/2010.. Its wrong

    Tuesday, June 01, 2010 2:35 PM
  • Hi,

    that's not really possible in regex (I think), because of the definition of leap years.

    You have to check several things:
    1. is the year divisible by 4 without remainder
    2. is the year divisible by 100 without remainder
    3. is the year divisible by 400 without remainder

    if 1 is true, it might be a leap year (check #2), otherwise it's no leap year
    if 2 is also true, it might be a leap year (check #3), otherwise it is a leap year
    if 3 is also true, it is a leap year, otherwise it's not

    So 1900 was not a leap year but 2000 was one

    1900/4=475
    1900/100=19
    1900/400=4.75

    2000/4=500
    2000/100=20
    2000/400=5.00

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    Tuesday, June 01, 2010 3:24 PM
  • (?x)
    (?:0[1-9]|1[0-2]) #month
    (/?)
    (?:0[1-9]|[12][0-9]|3[01]) #day
    (\1)
    \d{4} #year
    |
    \d/\d/\d{4}

    Regards
    Tuesday, June 01, 2010 3:30 PM