How to calculate number of day name between 2 dates in javascript

前端 未结 1 762
长情又很酷
长情又很酷 2020-12-21 20:04

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 b

1条回答
  •  生来不讨喜
    2020-12-21 20:31

    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);
    

    0 讨论(0)
提交回复
热议问题