How can I determine whether a given string represents a date?

后端 未结 11 1624
一个人的身影
一个人的身影 2020-12-11 00:31

Is there an isDate function in jQuery?

It should return true if the input is a date, and false otherwise.

相关标签:
11条回答
  • 2020-12-11 01:17

    simplest way in javascript is:

    function isDate(dateVal) {
      var d = new Date(dateVal);
      return d.toString() === 'Invalid Date'? false: true;
    }
    
    0 讨论(0)
  • 2020-12-11 01:17

    There's no built-in date functionality in jQuery core...and it doesn't really do anything directly to help with dates, so there aren't many libraries on top of it (unless they're date pickers, etc). There are several JavaScript date libraries available though, to make working with them just a bit easier.

    I can't answer for sure what's best...it depends how they're entering it and what culture you're dealing with, keep in mind that different cultures are used to seeing their dates in different format, as a quick example, MM/DD/YYYY vs YYYY/MM/DD (or dozens of others).

    0 讨论(0)
  • 2020-12-11 01:19

    I guess you want something like this. +1 if it works for you.

    HTML

     Date : <input type="text" id="txtDate" /> (mm/dd/yyyy)
     <br/><br/><br/>
    <input type="button" value="ValidateDate" id="btnSubmit"/>
    

    jQuery

    $(function() {
    $('#btnSubmit').bind('click', function(){
        var txtVal =  $('#txtDate').val();
        if(isDate(txtVal))
            alert('Valid Date');
        else
            alert('Invalid Date');
    });
    
    function isDate(txtDate)
    {
    var currVal = txtDate;
    if(currVal == '')
        return false;
    
    var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
    var dtArray = currVal.match(rxDatePattern); // is format OK?
    
    if (dtArray == null) 
        return false;
    
    //Checks for mm/dd/yyyy format.
    dtMonth = dtArray[1];
    dtDay= dtArray[3];
    dtYear = dtArray[5];        
    
    if (dtMonth < 1 || dtMonth > 12) 
        return false;
    else if (dtDay < 1 || dtDay> 31) 
        return false;
    else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
        return false;
    else if (dtMonth == 2) 
    {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay> 29 || (dtDay ==29 && !isleap)) 
                return false;
    }
    return true;
    }
    
    });
    

    CSS

    body{
    font-family:Tahoma;
    font-size : 8pt;
    padding-left:10px;
    }
    input[type="text"]
    {
    font-family:Tahoma;
    font-size : 8pt;
    width:150px;
    }
    

    DEMO

    0 讨论(0)
  • 2020-12-11 01:20

    You should use moment.js it's the best lib to handle all kind of dates. Solution to your problem:

    var inputVal = '2012-05-25';
    moment(inputVal , 'YYYY-MM-DD', true).isValid();
    
    0 讨论(0)
  • 2020-12-11 01:25

    Date.parse will prb sort you out, without the need for jquery:

    http://www.w3schools.com/jsref/jsref_parse.asp

    Above removed after some kind soul pointed out how basic parseDate really is.

    There is also a $.datepicker.parseDate( format, value, options ) utility function in the JQuery UI Datepicker plugin:

    https://api.jqueryui.com/datepicker/

    0 讨论(0)
提交回复
热议问题