Count Weekdays Between Two Dates

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore a JavaScript program that counts the number of weekdays between two given dates. The program uses an array and the reduce method to iterate over the given range of dates, checking if each date is a weekday and incrementing the count accordingly. However, it should be noted that this program does not take official holidays into account.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} javascript/data_types -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} javascript/arith_ops -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} javascript/comp_ops -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} javascript/cond_stmts -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} javascript/higher_funcs -.-> lab-28240{{"`Count Weekdays Between Two Dates`"}} end

Count Weekdays Between Two Dates

To count the weekdays between two dates, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Array.from() to create an array with a length equal to the number of days between startDate and endDate.
  3. Use Array.prototype.reduce() to iterate over the array, checking if each date is a weekday and incrementing count.
  4. Update startDate with the next day each loop using Date.prototype.getDate() and Date.prototype.setDate() to advance it by one day.
  5. Please note that this function does not take official holidays into account.

Here's the code to implement this:

const countWeekDaysBetween = (startDate, endDate) =>
  Array.from({ length: (endDate - startDate) / (1000 * 3600 * 24) }).reduce(
    (count) => {
      if (startDate.getDay() % 6 !== 0) count++;
      startDate = new Date(startDate.setDate(startDate.getDate() + 1));
      return count;
    },
    0
  );

You can use the following code to test the function:

countWeekDaysBetween(new Date("Oct 05, 2020"), new Date("Oct 06, 2020")); // 1
countWeekDaysBetween(new Date("Oct 05, 2020"), new Date("Oct 14, 2020")); // 7

Summary

Congratulations! You have completed the Count Weekdays Between Two Dates lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like