Add Weekdays to Date

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore a JavaScript function that helps calculate the date after adding a given number of business days. The function uses array manipulation and date iteration to increment the start date while taking weekends into account. This lab will help you understand how to manipulate dates in JavaScript and apply business logic to date calculations.


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`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/data_types -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/arith_ops -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/comp_ops -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/cond_stmts -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/higher_funcs -.-> lab-28129{{"`Add Weekdays to Date`"}} javascript/destr_assign -.-> lab-28129{{"`Add Weekdays to Date`"}} end

Function to Add Business Days to a Given Date

To calculate a future date by adding a given number of business days, you can use the addWeekDays function. Here are the steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the addWeekDays function that takes two arguments: startDate and count.
  3. startDate is the date from which you want to start adding business days.
  4. count is the number of business days you want to add to the start date.
  5. The function constructs an array using Array.from() method and sets the length equal to the count of business days to be added.
  6. Array.prototype.reduce() method is used to iterate over the array, starting from startDate, and incrementing it using Date.prototype.getDate() and Date.prototype.setDate().
  7. The function checks whether the current date is on a weekend or not.
  8. If the current date is on a weekend, the function updates it again by adding either one day or two days to make it a weekday.
  9. The function does not take official holidays into account.
const addWeekDays = (startDate, count) =>
  Array.from({ length: count }).reduce((date) => {
    date = new Date(date.setDate(date.getDate() + 1));
    if (date.getDay() % 6 === 0)
      date = new Date(date.setDate(date.getDate() + (date.getDay() / 6 + 1)));
    return date;
  }, startDate);

Here are some examples of how you can use the addWeekDays function:

addWeekDays(new Date("Oct 09, 2020"), 5); // 'Oct 16, 2020'
addWeekDays(new Date("Oct 12, 2020"), 5); // 'Oct 19, 2020'

Summary

Congratulations! You have completed the Add Weekdays to Date lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like