Javascript 如何验证 MM/dd/yyyy hh:mm 格式的 DateTime?

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

How to validate the DateTime of MM/dd/yyyy hh:mm format?

javascriptasp.netregex

提问by Lakshmitha

I am using MaskedEditExtender for entering a datetime. I am unable to figure out how to validate it.

我正在使用 MaskedEditExtender 输入日期时间。我无法弄清楚如何验证它。

Is there any Regular Expression for validating dates along with time MM/dd/yyyy hh:mm

是否有任何正则表达式用于验证日期以及时间 MM/dd/yyyy hh:mm

or any Javascript function ??

或任何 Javascript 函数?

回答by Harun

THis will solve your issue:

这将解决您的问题:

^(([0]?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/[1-2]\d{3}) (20|21|22|23|[0-1]?\d{1}):([0-5]?\d{1})$

回答by mplungjan

Javascript has Date.parse

Javascript 有Date.parse

it takes US formatted date of mm/dd/yyyy hh:mm:ss

它需要美国格式的日期 mm/dd/yyyy hh:mm:ss

alert(new Date(Date.parse("09/10/2011 12:00")))

alert(new Date(Date.parse("09/10/2011 12:00")))

will return 10th September 2011 at noon

将于 2011 年 9 月 10 日中午返回

回答by Oded

Use DateTime.Parseor DateTime.TryParse(there are also ParseExactand TryParseExactequivalents).

使用DateTime.ParseDateTime.TryParse(也有ParseExactTryParseExact等价物)。

If the string does not represent a valid DateTimeit will not parse.

如果字符串不代表有效DateTime,则不会解析。

DateTime myDateTime = DateTime.ParseExact(myString, 
                                          "MM/dd/yyyy hh:mm", 
                                          CultureInfo.InvariantCulture);

The above will throw an exception if the value is not parseable. Use the Tryvariant if you want to avoid the chance of the exception being thrown - this requires an outparameter and testing the return value of the function for success.

如果该值不可解析,则上述内容将引发异常。Try如果您想避免抛出异常的机会,请使用该变体 - 这需要一个out参数并测试函数的返回值是否成功。

回答by Alexander Tsepkov

And just in case you want the regular expression, this should work:

以防万一你想要正则表达式,这应该有效:

^(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d ([01]\d|2[0-3]):[0-5]\d$

回答by user2160658

You may try following Function that Validates date in "dd/MM/yyyy HH:mm"format

您可以尝试按照“dd/MM/yyyy HH:mm”格式验证日期的功能

    function ValidateDate(dt) {
        try {
            var isValidDate = false;
            var arr1 = dt.split('/');
            var year=0;var month=0;var day=0;var hour=0;var minute=0;var sec=0;
            if(arr1.length == 3)
            {
                var arr2 = arr1[2].split(' ');
                if(arr2.length == 2)
                {
                    var arr3 = arr2[1].split(':');
                    try{
                        year = parseInt(arr2[0],10);
                        month = parseInt(arr1[1],10);
                        day = parseInt(arr1[0],10);
                        hour = parseInt(arr3[0],10);
                        minute = parseInt(arr3[1],10);
                        //sec = parseInt(arr3[0],10);
                        sec = 0;
                        var isValidTime=false;
                        if(hour >=0 && hour <=23 && minute >=0 && minute<=59 && sec >=0 && sec<=59)
                            isValidTime=true;
                        else if(hour ==24 && minute ==0 && sec==0)
                            isValidTime=true;

                        if(isValidTime)
                        {
                            var isLeapYear = false;
                            if(year % 4 == 0)
                                 isLeapYear = true;

                            if((month==4 || month==6|| month==9|| month==11) && (day>=0 && day <= 30))
                                    isValidDate=true;
                            else if((month!=2) && (day>=0 && day <= 31))
                                    isValidDate=true;

                            if(!isValidDate){
                                if(isLeapYear)
                                {
                                    if(month==2 && (day>=0 && day <= 29))
                                        isValidDate=true;
                                }
                                else
                                {
                                    if(month==2 && (day>=0 && day <= 28))
                                        isValidDate=true;
                                }
                            }
                        }
                    }
                    catch(er){isValidDate = false;}
                }

            }

            return isValidDate;
        }
        catch (err) { alert('ValidateDate: ' + err); }
    }