Skip to content Skip to sidebar Skip to footer

How To Calculate Number Of Day Name Between 2 Dates In Javascript

I have 2 datepickers and a list of checkboxes of week days. A user can select a start date or end date and check any checkbox day. I want to count the number of week days between

Solution 1:

You need to iterate between dates and check the day

First convert the dates into a date object

date1 = convertToDateObj(date1); //assuming you already have a way to parse this string to date
date2 = convertToDateObj(date2); //assuming you already have a way to parse this string to date

Now iterate throught them

var dayCount = {0:0,1:0,2:0,3:0,4:0,5:0,6:0}; //0 is sunday and 6 is saturday
for (var d = date1; d <= date2; d.setDate(d.getDate() + 1)) 
{
    dayCount[d.getDay()]++;
}
console.log(dayCount);

Post a Comment for "How To Calculate Number Of Day Name Between 2 Dates In Javascript"