简体   繁体   中英

How to check date should not be past date for current date in javascript?

i am using jquery datepicker.i have to validate that selected date should not be past date from current date like given below

from 2015-06-12 to 2015-05-12

how to validate this ...

Just use: Date.parse(date) > Date.now() for comparison to now, or Date.parse(comparableDate1) > Date.parse(comparableDate2) for general comparison

Here's a JSFiddle

Here you can find a fiddle example for doing this: http://jsfiddle.net/H3H8s/5/

Script :

if(dateCheck("02/05/2013","02/09/2013","02/07/2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

Since you're using JS Datepicker, you can restrict the min-date to current date.

Here's a JSFiddle

$(function(){
var d = new Date();
$('#thedate').datepicker({
    dateFormat: 'dd-mm-yy',
    minDate:"-1d",
    maxDate:"d"
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM