11

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

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Gopesh
  • 3,882
  • 11
  • 37
  • 52
  • you can go [here](http://stackoverflow.com/questions/5465375/javascript-date-regex-dd-mm-yyyy) for previously answered question – cctan Jan 20 '12 at 06:16

11 Answers11

34

function parseDate(str) {
  var m = str.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/);
  return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}


Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • I needed similar which allows (4/6/17 , 04/6/2017, 4/06/17, 04/06/2017 , 04/06/17 etc) so I just changed it bit: /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/ – Renascent Apr 20 '18 at 09:26
15

Notice

Your regexp does not work for years that "are multiples of 4 and 100, but not of 400". Years that pass that test are not leap years. For example: 1900, 2100, 2200, 2300, 2500, etc. In other words, it puts all years with the format \d\d00 in the same class of leap years, which is incorrect. – MuchToLearn

So it works properly only for [1901 - 2099] (Whew)

dd-MM-yyyy

Checks if leap year. Years from 1900 to 9999 are valid. Only dd-MM-yyyy

var stringToValidate = "29-02-2012";
var rgexp = /(^(((0[1-9]|1[0-9]|2[0-8])[-](0[1-9]|1[012]))|((29|30|31)[-](0[13578]|1[02]))|((29|30)[-](0[4,6,9]|11)))[-](19|[2-9][0-9])\d\d$)|(^29[-]02[-](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)$)/;
var isValidDate = rgexp.test(stringToValidate);
gdZeus
  • 1,081
  • 8
  • 7
8

Here is Regex for multiple date formats working for me :

        //dd.MM.yyyy
        var date_regex = /^(0[1-9]|1\d|2\d|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}$/;
        alert(date_regex.test("02.02.1991"));  

//      //dd/mm/yyyy
//      var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/(19|20)\d{2}$/;
//          alert(date_regex.test("02/12/1991"));  

//      //dd-mm-yyyy
//      var date_regex = /^(0[1-9]|1\d|2\d|3[01])\-(0[1-9]|1[0-2])\-(19|20)\d{2}$/;
//      alert(date_regex.test("02-12-1991")); 

//      //mm/dd/yyyy
//      var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
//      alert(date_regex.test("12/02/1991")); 


//      //yyyy.MM.dd
//      var date_regex = /^((19|20)\d{2})\.(0[1-9]|1[0-2])\.(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991.12.02")); 

//      //yyyy/MM/dd
//      var date_regex = /^((19|20)\d{2})\/(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991/12/02")); 

//      //yyyy-MM-dd
//      var date_regex = /^((19|20)\d{2})\-(0[1-9]|1[0-2])\-(0[1-9]|1\d|2\d|3[01])$/;
//      alert(date_regex.test("1991-12-02"));
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
4

Try this:

'01-01-2012'.match( /\d{2}-\d{2}-\d{4}/ )

Note that that this way the date 33-12-2022 would be considered valid as well!

zaphod1984
  • 836
  • 2
  • 7
  • 22
3
'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).

Dan
  • 736
  • 8
  • 12
3

Well, I made this:

'31-12-1987'.match(/(3[01]|[2][0-9]|0\d)-(1[0-2]|0\[1-9])-\d{4}/)

Validates the day from 01 to 31, month from 01 to 12 and year of four digits. It only fails the february 30, and the months without 31 days. Which you can clean using the new Date('mm/dd/yyyy').

Davsket
  • 1,248
  • 1
  • 9
  • 14
  • for hijri date , there is no `31`ˢᵗ .. that's why , we remove `1` from `/3[01]....` to have : `/(3[0]|[2][0-9]|0\d)-(1[0-2]|0\[1-9])-\d{4}/` – Abdennour TOUMI Jan 12 '17 at 07:10
2

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}$/;
Rawat
  • 21
  • 2
0

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;
}
Jesse Sanford
  • 607
  • 1
  • 8
  • 18
0
$('#DOB').blur(function ()   {
var s = $('#DOB').val();   alert('Entered date is:' + s);
var parms = s.split(/[\.\-\/]/);
var yyyy = parseInt(parms[2], 10);

var d = new Date();
var n = d.getFullYear(); //alert('current year is :' + n);
if (yyyy > n || yyyy < 1900) {
    alert('Improper date format, Please enter dd/mm/yyyy format. (invalid year)');
}
var mm = parseInt(parms[1], 10); 
if (mm > 12 || mm < 0)
{
    alert('Improper date format, Please enter dd/mm/yyyy format. (invalid month');
}
var dd = parseInt(parms[0], 10);
if (dd > 31 || dd < 0)
{
    alert('Improper date format, Please enter dd/mm/yyyy format. (invalid day');
}

//var date = new Date(dd, mm - 1, yyyy, 12, 0, 0, 0);
//var ndate = (date.getMonth() + 1) && ddmm === date.getDate() &&  yyyy === date.getFullYear();
// alert('new date is:' + ndate);
});
ADyson
  • 57,178
  • 14
  • 51
  • 63
0

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$')
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
-2

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

vks
  • 9