Javascript 中日期格式的正则表达式 - dd-mm-yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8937408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:08:40  来源:igfitidea点击:

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

javascript

提问by Gopesh

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

我需要一个日期格式的正则表达式:Javascript 中的 dd-mm-yyyy。

回答by Sudhir Bastakoti


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;
}


回答by gdZeus

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

您的正则表达式在“是 4 和 100 的倍数,但不是 400”的年份中不起作用。通过该测试的年份不是闰年。例如:1900, 2100, 2200, 2300, 2500等,也就是说把\d\d00格式的所有年份都放在闰年的同一类中,这是错误的。– 多学习

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

所以它只适用于 [1901 - 2099] (Whew)

dd-MM-yyyy

dd-MM-yyyy

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

检查是否为闰年。从 1900 年到 9999 年是有效的。只有 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);

回答by zaphod1984

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!

请注意,这样日期 33-12-2022 也将被视为有效!

回答by Ninad Pingale

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"));

回答by Dan

'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).

这仅查找从 1800 年到 2099 年的有效日期。不支持闰年(因为它假设每年都是可能的闰年)。

回答by Davsket

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').

验证从 01 到 31 的日期、从 01 到 12 的月份和四位数的年份。它只在 2 月 30 日和没有 31 天的月份失败。您可以使用new Date('mm/dd/yyyy').

回答by Rawat

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

此正则表达式适用于 MM/DD/YYYY 和 M/D/YYYY

var date_regex = /^(0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/]\d{4}$/;

回答by Jesse Sanford

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

将以上几个一起工作(主要是@gdZeus)现在你可以做 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.

此外,如果您在 js 文件中使用此内联,则可以使用以下返回正则表达式文字的内容。这将允许您验证日期是过去的日期!这对生日很方便。您也可以将其反转以检查某个日期是否在未来(例如检查信用卡 exp)。这几乎可以在 javascript 中的任何地方使用,但如果您真的需要正则表达式文字,则不行。例如,如果您将其序列化为其他格式而无法运行 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;
}

回答by user7012374

$('#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);
});

回答by San Jaisy

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$')