jQuery function to compare date with current date

前端 未结 5 1889
生来不讨喜
生来不讨喜 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
    }
    

提交回复
热议问题