简体   繁体   English

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

[英]Regular expression for date format- dd-mm-yyyy in Javascript

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


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

Notice 注意

Your regexp does not work for years that "are multiples of 4 and 100, but not of 400". 你的正则表达式在“4和100的倍数,但不是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. 例如:1900,2100,2200,2300,2500等。换句话说,它将格式\\ d \\ d00的所有年份放在同一类闰年中,这是不正确的。 – MuchToLearn - MuchToLearn

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. 从1900年到9999年是有效的。 Only dd-MM-yyyy 只有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);

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也被认为是有效的!

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"));
'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]/ )

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

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. 验证从01到31的月份,从01到12的月份以及四位数的年份。 It only fails the february 30, and the months without 31 days. 它只在2月30日和没有31天的月份失败。 Which you can clean using the new Date('mm/dd/yyyy') . 您可以使用new Date('mm/dd/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}$/;

Working a few of the above together (primarily @gdZeus's) now you can do MM/dd/yyyy | 现在你可以一起工作上面的一些(主要是@gdZeus),你可以做MM / dd / yyyy | MM-dd-yyyy | 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. 此外,如果您在js文件中使用此内联,则可以使用以下命令返回正则表达式文字。 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). 您可以将其反转以检查日期是否在将来(例如,检查信用卡exp)。 This will work almost anywhere in javascript but not if you really need a regexp literal. 这几乎可以在javascript的任何地方工作,但如果你真的需要regexp文字则不行。 For example if you are serializing it to a some other format without the ability to run js. 例如,如果您将其序列化为某种其他格式而无法运行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;
}
$('#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);
});

这适合我

new RegExp('^(0[1-9]|[1-9]|[12][0-9]|3[01])-(0[1-9]|[1-9]|1[012])-(19|20)\\d\\d$')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM