0

I populate 2 dates 03-Mar-2015 and 03-Jun-2015, if Last 3 months option is selected.I perform the below JavaScript validation to check for 90 days. But it shows validation error that the two dates selected are greater than 90 days. But my constraint is not allow users to select more than 3 months duration.

function DtTimeDiff(sender, args) {
        var startDate = Date.parse(document.getElementById('ctl00$MainContent$FromYearTxt').value);
        var endDate = Date.parse(document.getElementById('ctl00$MainContent$ToYearTxt').value);
        var timeDiff = endDate - startDate;

        daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

        if (daysDiff > 90) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }

    }

How to perform validation for 3 months having 91 days(Mar,Apr,May) and 92 days (Jul,Aug,Sep)? Constrain is not allow users to select more than 3 months duration.

sqluser
  • 5,502
  • 7
  • 36
  • 50
Marid
  • 83
  • 10
  • possible duplicate of [How do i find the difference between two dates using jquery](http://stackoverflow.com/questions/10225268/how-do-i-find-the-difference-between-two-dates-using-jquery) – Kurenai Kunai Jun 04 '15 at 04:32

2 Answers2

0

You can try calculate month and date separately

function DtTimeDiff(sender, args) {
    startDate = new Date("03-03-2015");/*Assume format is MM-DD-YYYY*/
    endDate = new Date("06-01-2015");/*Assume format is MM-DD-YYYY*/
    var timeDiff = endDate - startDate;

    startDateMonth = startDate.getMonth() + 1;
    endDateMonth = endDate.getMonth() + 1;

    startDateDate = startDate.getDate();
    endDateDdate = endDate.getDate();

    if (endDateMonth - startDateMonth >= 3) {
        if (endDateDdate - startDateDate >= 0) {
            args.IsValid = true;
            console.log("In");
        } else {
            args.IsValid = false;
            console.log("Out");
        }
    }
}
A Web-Developer
  • 868
  • 8
  • 29
0

You can add the intermediate code to compute the month duration based on the dates:

function DtTimeDiff() {
    var startDate = new Date(document.getElementById('ctl00$MainContent$FromYearTxt').value);
    var endDate = new Date(document.getElementById('ctl00$MainContent$ToYearTxt').value);

    var monthsDiff = endDate.getMonth() - startDate.getMonth();
    var durationLimit = 0;
    for (i = 1; i <= monthsDiff; i++) {
        durationLimit += new Date(startDate.getFullYear(), startDate.getMonth() + i, 0).getDate();
    }

    var timeDiff = endDate.getTime() - startDate.getTime();
    var daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

    if (daysDiff > durationLimit) {
        args.IsValid = false;
    } else {
        args.IsValid = true;
    }
}
avdhut
  • 128
  • 1
  • 13
  • Working as expected. Thanks avdhut – Marid Jun 04 '15 at 05:19
  • The script is failing for the scenario : start date 09/12/2009 end date 10/15/2009. The date difference is not more than 90 days, but still fails. any help – Marid Jun 25 '15 at 06:24