Thursday, 2 February 2012

JQuery Date Comparison

function IsStartDateGreater(startDate, endDate) {

    var RetVal = false;
    var StartDate;
    var EndDate;

    startDate = $.datepicker.parseDate("dd/mm/yy", ReplaceAll(startDate, ".", "/"));
    endDate = $.datepicker.parseDate("dd/mm/yy", ReplaceAll(endDate, ".", "/"));

    StartDate = new Date(startDate);
    EndDate = new Date(endDate);

    if (StartDate > EndDate) {
        RetVal = true;
    }

    return RetVal;
}

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = -1;

    if (temp) {
        index = temp.indexOf(stringToFind);
        while (index != -1) {
            temp = temp.replace(stringToFind, stringToReplace);
            index = temp.indexOf(stringToFind);
        }
    }
    return temp;
}

Ex:

IsStartDateGreater(“25.02.2012”, “15.05.2010”); //true

IsStartDateGreater(“25/02/2012”, “15/05/2010”); //true

IsStartDateGreater(“13.04.2010”, “23.05.2009”); //false

IsStartDateGreater(“13/04/2010”, “23/05/2009”); //false

JQuery check a valid Date

function IsValidDate(val) {

    var RetVal = false;
    var ValidDate;

    try {

        ValidDate = $.datepicker.parseDate("dd/mm/yy", ReplaceAll(val, ".", "/"));
        RetVal = true;

    }
    catch (ex) {
    }

    return RetVal;
}

Ex:

IsValidDate(“25.02.2010”);    //true

IsValidDate(“25/02/2010”);    //true

IsValidDate(“25022010”);      //false