Regular expression for date format- dd-mm-yyyy in Javascript

后端 未结 11 1557
南笙
南笙 2020-12-09 21:14

I need a regular expression for date format: dd-mm-yyyy in Javascript.

相关标签:
11条回答
  • 2020-12-09 21:42

    This works for me

    new RegExp('^(0[1-9]|[1-9]|[12][0-9]|3[01])-(0[1-9]|[1-9]|1[012])-(19|20)\\d\\d$')
    
    0 讨论(0)
  • 2020-12-09 21:43

    Working a few of the above together (primarily @gdZeus's) now you can do MM/dd/yyyy | MM-dd-yyyy | MM.dd.yyyy

    /(^(((0[1-9]|1[012])[-/.](0[1-9]|1[0-9]|2[0-8]))|((0[13578]|1[02])[-/.](29|30|31))|((0[4,6,9]|11)[-/.](29|30)))[-/.](19|[2-9][0-9])\d\d$)|(^02[-/.]29[-/.](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/
    

    Additionally if you are using this inline in a js file you can use the following which returns a regexp literal. This will allow you to validate that a date is in the past! This is handy for birthdays. You can reverse it to check that a date is in the future as well (ex. checking credit card exp). This will work almost anywhere in javascript but not if you really need a regexp literal. For example if you are serializing it to a some other format without the ability to run js.

    new RegExp('(^(((0[1-9]|1[012])[-/.](0[1-9]|1[0-9]|2[0-8]))|((0[13578]|1[02])[-/.](29|30|31))|((0[4,6,9]|11)[-/.](29|30)))[-/.]('+range(1920, new Date().getFullYear()).join('|')+')$)|(^02[-/.]29[-/.]('+range(1920, new Date().getFullYear()).filter(function(year){if (year % 4 == 0) { return true }}).join('|')+')$)/', 'g')
    

    returns:

    /(^(((0[1-9]|1[012])[-\/.](0[1-9]|1[0-9]|2[0-8]))|((0[13578]|1[02])[-\/.](29|30|31))|((0[4,6,9]|11)[-\/.](29|30)))[-\/.](1920|1921|1922|1923|1924|1925|1926|1927|1928|1929|1930|1931|1932|1933|1934|1935|1936|1937|1938|1939|1940|1941|1942|1943|1944|1945|1946|1947|1948|1949|1950|1951|1952|1953|1954|1955|1956|1957|1958|1959|1960|1961|1962|1963|1964|1965|1966|1967|1968|1969|1970|1971|1972|1973|1974|1975|1976|1977|1978|1979|1980|1981|1982|1983|1984|1985|1986|1987|1988|1989|1990|1991|1992|1993|1994|1995|1996|1997|1998|1999|2000|2001|2002|2003|2004|2005|2006|2007|2008|2009|2010|2011|2012|2013|2014|2015)$)|(^02[-\/.]29[-\/.](1920|1924|1928|1932|1936|1940|1944|1948|1952|1956|1960|1964|1968|1972|1976|1980|1984|1988|1992|1996|2000|2004|2008|2012)$)\//g
    

    NOTE: this utilizes underscore's range function to generate the dates. You can write your own though like this very inelegant version :)

    function range(start, end) {
      var foo = [];
      for (var i = start; i <= end; i++) {
        foo.push(i);
      }
      return foo;
    }
    
    0 讨论(0)
  • 2020-12-09 21:51

    This regex is for MM/DD/YYYY and M/D/YYYY

    var date_regex = /^(0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/]\d{4}$/;
    
    0 讨论(0)
  • 2020-12-09 21:51

    /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})\$/

    0 讨论(0)
  • 2020-12-09 21:55
    '01-01-2012'.match( /(?!3[2-9]|00|02-3[01]|04-31|06-31|09-31|11-31)[0-3][0-9]-(?!1[3-9]|00)[01][0-9]-(?!10|28|29)[12][089][0-9][0-9]/ )
    

    This looks for only valid dates from 1800 to 2099. No leap year support (as in it assumes every year is a possible leap year).

    0 讨论(0)
提交回复
热议问题