Skip to content Skip to sidebar Skip to footer

Find The Number Of Sunday Between Two Dates And If Sunday Comes Between Two Dates Then Substract The Sunday

In this JavaScript I am able to calculate the difference two dates but am not able to find the Sunday between dates. If Sunday occurs between two dates then subtract the Sunday fro

Solution 1:

You should use the Javascript Date Object and more especially the getDay() method.

Then you can increment your start date until end date and count the Sundays you encounter, something like :

var start = $.datepicker.parseDate('dd M y', $(".datepicker").val());
var end = $.datepicker.parseDate('dd M y', $(".datepicker_2").val());

var startDate = newDate(start);
var endDate = newDate(end);
var totalSundays = 0;

for (var i = startDate; i <= endDate; ){
    if (i.getDay() == 0){
        totalSundays++;
    }
    i.setTime(i.getTime() + 1000*60*60*24);
}

console.log(totalSundays);

Nota Bene :

I did not understand your "substraction rule"

Post a Comment for "Find The Number Of Sunday Between Two Dates And If Sunday Comes Between Two Dates Then Substract The Sunday"