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 2 days.

For example: I want to join any yoga classes then I will select start or end date and also select week day like Monday , Tuesday .

Now i want to count the number of all Mondays and Tuesdays between 2 dates

 date1 = Mar 01,2016 
 date2 = Apr 01,2016 

I want to count number of day name between these date like this:

no of sunday: 4
no of monday: 4
no of tuesday: 5 etc..

I have tried this code

var d           = new Date(date1);
var now         = new Date(Date.now());
var daysOfYear  = [];
count           = 0;
for (d ; d <= date1 ; d.setDate(d.getDate() + 1)) {
  val = $("#ch_"+d.getDay());           
  if(val.is(':checked')){
    count++;
  }
}

But it gives a TypeError: d.getDay is not a function


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);
链接地址: http://www.djcxy.com/p/6250.html

上一篇: 在两个日期之间得到几个月,无视日子

下一篇: 如何在JavaScript中计算2个日期之间的日期名称数量