Thursday, February 22, 2018

Check Date within Date Range

Here the way you can compare one date between two dates in java script.

Date format here is like YYYY-MMM-DD (2015-Sep-12)


if(dateCheck("2012-Sep-12","2017-Jan-14","2012-Sep-11"))
    alert("Ok Match");
else
    alert("Not Match");

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;
}

The below one if you have different time format you Date constructor and achieve the above task.
Date format is like YYYY-MM-DD (2018-01-30). And remember for month you should subtract it from one. I mean month-1. Since month starting from number 0.

var parts1 = from.split('-');
    var parts2 = to.split('-');
    var parts3 = check.split('-');
    
    fDate = new Date(parts1[0], parts1[1] - 1, parts1[2]); 
    lDate = new Date(parts2[0], parts2[1] - 1, parts2[2]); 
    cDate = new Date(parts3[0], parts3[1] - 1, parts3[2]);

 console.log("From Date " + fDate.toDateString());
    console.log("Last Date " + lDate.toDateString());
    console.log("Check Date " + cDate.toDateString());


No comments:

Post a Comment