jQuery function to compare date with current date

前端 未结 5 1900
生来不讨喜
生来不讨喜 2020-12-06 22:47

i am entering date of birth in form by entering date in separate text box ,selecting year from drop down and entering year in text box. Now i want to check that entered date

相关标签:
5条回答
  • 2020-12-06 23:03

    Provided that your input values are integers, you can construct a Date instance using them:

    var CurrentDate = new Date();
    var SelectedDate = new Date(
        $('[id$=txtYear]').val(),
        $('[id$=drpMonth]').val(),
        $('[id$=spDate]').val()
    );
    
    //As quite rightly mentioned, January = 0, so if your inputs have the literal number for each month (1 for January) replace the line above with the following, to take a month off:
    //var SelectedDate = new Date($('[id$=txtYear]').val(), $('[id$=drpMonth]').val()-1, $('[id$=spDate]').val());
    
    if(CurrentDate > SelectedDate){
        //CurrentDate is more than SelectedDate
    }
    else{
        //SelectedDate is more than CurrentDate
    }
    
    0 讨论(0)
  • 2020-12-06 23:04
      function compareDate() {
    var dateEntered = document.getElementById("txtDateEntered").value; 
    var date = dateEntered.substring(0, 2);
    var month = dateEntered.substring(3, 5);
    var year = dateEntered.substring(6, 10);
    
    var dateToCompare = new Date(year, month - 1, date);
    var currentDate = new Date();
    
    if (dateToCompare > currentDate) {
        alert("Date Entered is greater than Current Date ");
    }
    else {
        alert("Date Entered is lesser than Current Date");
    }
    }
    
    0 讨论(0)
  • 2020-12-06 23:12

    Comparing Date objects is trivial, just use the standard relational operators. When passed an Object they implicitly call the object's .valueOf() function which will return the time in milliseconds, suitable for comparison.

    The hard part is constructing a valid Date object in the first place.

    For reliability I strongly recommend using this version of the Date constructor rather than passing a string:

    new Date(year, month, day [, hour, minute, second, millisecond]);
    

    which ensures that the local date format doesn't matter. Passing a string can generate different results depending on whether the default date format is mm/dd/yyyy or dd/mm/yyyy in the user's locale.

    Don't forget that the year must be a full digit year, and that in this version January is represented by 0, not 1:

    var SelectedDate = new Date(
        $('[id$=txtYear]').val(),
        $('[id$=drpMonth]').val() - 1,
        $('[id$=spDate]').val());
    
    if (CurrentDate > SelectedDate) { ... }
    
    0 讨论(0)
  • 2020-12-06 23:15

    Check if this can help:

    var TodayDate = new Date();
    var endDate= new Date(Date.parse($("#EndDate").val()));
    
    if (endDate> TodayDate) {
        // throw error here..
    }
    

    https://stackoverflow.com/a/40039798/885627

    0 讨论(0)
  • 2020-12-06 23:17

    Try this function:

    function isFutureDate(dateText) {
    
                var arrDate = dateText.split("/");
                var today = new Date();
                useDate = new Date(arrDate[2], arrDate[1] - 1, arrDate[0]);
                if (useDate > today) {
                    return true;
                } else return false;
            }
    
    0 讨论(0)
提交回复
热议问题