3

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 not be greater that current date in jquery. Please help how?

var CurrentMonth = date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear();
    var SelectedDate = $('[id$=spDate]').val() + '/' + $('[id$=drpMonth]').val() + '/' + $('[id$=txtYear]').val();
    if (CurrentDate > SelectedDate) {
        return false;
    } 
jww
  • 97,681
  • 90
  • 411
  • 885
Supreet
  • 2,451
  • 8
  • 25
  • 42

6 Answers6

13

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
}
George
  • 36,413
  • 9
  • 66
  • 103
3

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) { ... }
Alnitak
  • 334,560
  • 70
  • 407
  • 495
3
  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");
}
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • This is a function to check whether the rntered date is greater or less than the current date.For more details you can visit here http://sharepoint-2010-world.blogspot.in/2013/11/how-to-compare-dates-with-current-date.html – srikanthbattula Nov 11 '13 at 06:04
3

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

Community
  • 1
  • 1
himanshupareek66
  • 766
  • 10
  • 22
  • 1
    Be aware that if you take this route, the "TodayDate" variable will be date + time. If you only want the date, you can add a second line to clear the time: TodayDate.setHours(0,0,0,0); – Keiki Nov 08 '21 at 20:12
1

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;
        }
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
1

the accepted answer checks by time.
as the input type is "date", the milliseconds are 000
to compare for day

var variable1 = new Date();
var variable2 = variable1.toISOString().split('T')[0];
var SelectedDate = new Date(
    $('[id$=txtYear]').val(),
    $('[id$=drpMonth]').val(),
    $('[id$=spDate]').val()
);
if (SelectedDate.getTime() < new Date(variable2).getTime()) {
    //input date less than todays date
}else{
    //input date bigger than todays date
}
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42