Skip to content Skip to sidebar Skip to footer

Jquery Function To Compare Date With Current Date

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 must n

Solution 1:

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

varCurrentDate = newDate();
varSelectedDate = newDate(
    $('[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
}

Solution 2:

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:

newDate(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:

varSelectedDate = newDate(
    $('[id$=txtYear]').val(),
    $('[id$=drpMonth]').val() - 1,
    $('[id$=spDate]').val());

if (CurrentDate > SelectedDate) { ... }

Solution 3:

functioncompareDate() {
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 = newDate(year, month - 1, date);
var currentDate = newDate();

if (dateToCompare > currentDate) {
    alert("Date Entered is greater than Current Date ");
}
else {
    alert("Date Entered is lesser than Current Date");
}
}

Solution 4:

Check if this can help:

varTodayDate = newDate();
var endDate= newDate(Date.parse($("#EndDate").val()));

if (endDate> TodayDate) {
    // throw error here..
}

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

Solution 5:

Try this function:

functionisFutureDate(dateText) {

            var arrDate = dateText.split("/");
            var today = newDate();
            useDate = newDate(arrDate[2], arrDate[1] - 1, arrDate[0]);
            if (useDate > today) {
                returntrue;
            } elsereturnfalse;
        }

Post a Comment for "Jquery Function To Compare Date With Current Date"