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

1 comment:

  1. After searching for days and reading all the answers from jquery forum and stackoverflow this was the only solution that really helped me.

    Thank you so much Ravi Kumar Gupta

    ReplyDelete