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

前端 未结 5 1752
无人及你
无人及你 2021-01-06 20:25

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

Is there any Regular Expression for validating dates along with

相关标签:
5条回答
  • 2021-01-06 20:41

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

        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); }
        }
    
    0 讨论(0)
  • 2021-01-06 20:52

    Use DateTime.Parse or DateTime.TryParse (there are also ParseExact and TryParseExact equivalents).

    If the string does not represent a valid DateTime it will not parse.

    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 Try variant if you want to avoid the chance of the exception being thrown - this requires an out parameter and testing the return value of the function for success.

    0 讨论(0)
  • 2021-01-06 20:53

    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$
    
    0 讨论(0)
  • 2021-01-06 20:55

    Javascript has Date.parse

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

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

    will return 10th September 2011 at noon

    0 讨论(0)
  • 2021-01-06 20:59

    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})$
    
    0 讨论(0)
提交回复
热议问题