Javascript 中 MM/DD/YYYY 的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6402743/
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-23 21:33:49  来源:igfitidea点击:

Regular Expression for MM/DD/YYYY in Javascript

javascriptregex

提问by Mark Sandman

I've just written this regular expression in javaScript however it doesn't seem to work, here's my function:

我刚刚在 javaScript 中编写了这个正则表达式,但它似乎不起作用,这是我的函数:

function isGoodDate(dt){
    var reGoodDate = new RegExp("/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/");
    return reGoodDate.test(dt);
}

and this is how I call it in my form validation

这就是我在表单验证中调用它的方式

if(!isGoodDate(userInput[1].value)){
           alert("date not in correct format of MM/dd/YYYY");
           return false;  
        }

now I want it to return MM/DD/YYYY however if I put a valid date in it raises the alert? Any ideas anyone?

现在我希望它返回 MM/DD/YYYY 但是如果我在其中输入有效日期会引发警报?任何人的想法?

回答by Jan

Attention, before you copy+paste: The question contains some syntactic errors in its regex. This answer is correcting the syntax. It is not claiming to be the best regex for date/time parsing.

注意,在复制+粘贴之前:该问题的正则表达式中包含一些语法错误。这个答案正在纠正语法。它并不声称是日期/时间解析的最佳正则表达式。

Try this:

尝试这个:

function isGoodDate(dt){
    var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
    return reGoodDate.test(dt);
}

You either declare a regular expression with:

您可以声明一个正则表达式:

new RegExp("^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$")

Or:

或者:

/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/

Notice the /

请注意 /

回答by Shef

Maybe because you are declaring the isGoodDate()function, and then you are calling the isCorrectDate()function?

也许是因为你声明了isGoodDate()函数,然后调用了isCorrectDate()函数?

Try:

尝试:

function isGoodDate(dt){
    var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
    return reGoodDate.test(dt);
}

Works like a charm, test it here.

就像一个魅力,在这里测试一下

Notice, this regex will validate dates from 01/01/1900through 31/12/2099. If you want to change the year boundaries, change these numbers (19|20)on the last regex block. E.g. If you want the year ranges to be from 01/01/1800through 31/12/2099, just change it to (18|20).

请注意,此正则表达式将验证从01/01/1900到 的日期31/12/2099。如果要更改年份边界,请(19|20)在最后一个正则表达式块上更改这些数字。例如,如果您希望年份范围从01/01/180031/12/2099,只需将其更改为(18|20)

回答by KooiInc

I don't think you need a regular expression for this. Try this:

我认为您不需要为此使用正则表达式。尝试这个:

function isGoodDate(dt){
    var dts  = dt.split('/').reverse()
       ,dateTest = new Date(dts.join('/'));
    return isNaN(dateTest) ? false : true;
}

//explained
    var dts  = dt.split('/').reverse()
//      ^ split input and reverse the result
//        ('01/11/2010' becomes [2010,11,01]
//        this way you can make a 'universal' 
//        datestring out of it
       ,dateTest = new Date(dts.join('/'));
//     ^ try converting to a date from the 
//       array just produced, joined by '/'
    return isNaN(dateTest) ? false : true;
//         ^ if the date is invalid, it returns NaN
//           so, if that's the case, return false

回答by mplungjan

I agree with @KooiInc, but it is not enough to test for NaN

我同意@KooiInc,但它不足以测试 NaN

function isGoodDate(dt){
    var dts  = dt.split('/').reverse()
       ,dateTest = new Date(dts.join('/'));
    return !isNaN(dateTest) && 
       dateTest.getFullYear()===parseInt(dts[0],10) &&
       dateTest.getMonth()===(parseInt(dts[1],10)-1) &&
       dateTest.getDate()===parseInt(dts[2],10) 
}

which will handle 29/2/2001 and 31/4/2011

这将处理 29/2/2001 和 31/4/2011



For this script to handle US dates do

对于这个脚本来处理美国日期做

function isGoodDate(dt){
    var dts  = dt.split('/')
       ,dateTest = new Date(dt);
    return !isNaN(dateTest) && 
       dateTest.getFullYear()===parseInt(dts[2],10) &&
       dateTest.getMonth()===(parseInt(dts[0],10)-1) &&
       dateTest.getDate()===parseInt(dts[1],10) 
}

回答by Usman Ali

Add this in your code, it working perfectly fine it here. click here http://jsfiddle.net/Shef/5Sfq6/

将此添加到您的代码中,它在这里工作得很好。点击这里http://jsfiddle.net/Shef/5Sfq6/

function isGoodDate(dt){
var reGoodDate = /^(?:(0[1-9]|1[012])[\/.](0[1-9]|[12][0-9]|3[01])[\/.](19|20)[0-9]{2})$/;
return reGoodDate.test(dt);

}

}

回答by Mostafa Khedeer

Validate (DD-MM-YYYY) format :)

验证 (DD-MM-YYYY) 格式 :)

function isGoodDate(dt) {
    var reGoodDate = /^(?:(0[1-9]|[12][0-9]|3[01])[\-.](0[1-9]|1[012])[\-.](19|20)[0-9]{2})$/;
    return reGoodDate.test(dt);
}