Calculate the Workday End Date in JavaScript

Given a start date, number of days, and holidays, this script calculates the end date of a workday period using JavaScript.

If you are looking to get a list of workdays in a range, then check out this tutorial:

Extract Workdays From a Date Range in JavaScript

The Example

Let’s say we have an array of starting dates for a work project and can estimate the number of days that each project will take, we want to determine the end date not including holidays and regular weekly days off, like weekends.

Let’s say our typical work week is Monday to Friday with Saturday and Sunday off.

We also have the following somewhat random holidays:

  • 15 Sep 2024
  • 31 Oct 2024  🎃🦇👻
  • 24 Dec 2024
  • 25 Dec 2024
  • 01 Jan 2025

Finally here are 3 sample example jobs that we need to calculate the last workday for:

  1. 10 Sep 2024 running for 10 workdays.
    End day for work project JavaScript example range 1
  2. 26 Oct 2024 running for 5 workdays.
    End day for work project JavaScript example range 2
  3. 22 Dec 2025 running for 16 workdays.
    End day for work project JavaScript example range 3

The blue represents the actual workdays, while the white section indicates the actual date range. Orange bold and underlined days are holidays that don’t fall on weekends. Light red days are weekly days off (in our case, weekends).

Across the top of the dates are the days of the week as a JavaScript day running from 0 (Sunday) to 8 (Saturday).

The Video

https://youtu.be/pJLlAE8REZY

To the video tutorial.

Releases 1 Oct 2024

Hire me for our next Google Workspace project.

The Code

The code above is divided into two functions:

The runsies() function

This is the main example run function containing the data samples including the array of start dates and number of required workdays along with the selected days off as an array of days off the week in JavaScript Date days format, and finally the array of holidays.

I’m using the year-month-date  format here, because…computers, but you could use other JS Date acceptable formats.

We then add these data samples to the getWorkdayEndDate function arguments. This will return an array of end dates the same length as the sample array. So we will iterate over the sample array and map our end dates to it before logging the samples array to see our results which will look like this:

The getWorkdayEndDate() function

Parameters

This function takes 3 arguments:

  1. workdays : An array of objects containing a startDate and number of workdays.
  2. weekends: The array of weekends to not count. These are depicted as JavaScript numbers with Sunday starting at zero (0) and ending with Saturday (6). You can add any day of the week here to exclude it from the weekdays.
  3. holidays: This is the array of date strings for each holiday to exclude. You may wish to generate this from a local holiday API relevant to your area and then include any additional days your company might have.

Main variables & UTC DAtes

We need to convert each of our dates into milliseconds to more efficiently compare our time-off periods against our start date and consecutive days.

Line 17 – First, we calculate a whole day in milliseconds.

Line 23-30 – To avoid any weirdness that might occur in our current timezone with any potential daylight-saving events, we will convert our date periods to  UTC (Coordinated Universal Time). This will occur a few times within our code so we will add a small private method up here at the top of the function to handle this.

You can see another example of UTC dates in practice here:

🐐You can support me for free by using this Amazon affiliate link in your next tech purchase :Computers & Stuff! 🐐

Get the Day of the Year in JavaScript

Typically, I would abstract this getUTC function out as a separate function or include it as a prototype, but you’re likely to just copy and paste the function so inside as a method it goes 😉.

Line 33 – Now we can convert the array of dates to UTC time in milliseconds.

Create an array of end dates

Line 38 – Next, we use the JavaScirpt map function to create an array of end dates by iterating over the workdays array.

For each workday, we need to set some variables that:

  1. Line 40 – dayStart : Set the start date to UTC format.
  2. Line 43 – dayOfWeek: Determine the first day of the week for the start day and then allow it to be updated each day.
  3. Line 44 – daysRemaining: Set the first day remaining to the number of workdays. We will subtract from this every time we find a workday.
  4. Line 45 – day: Set the current day to the dayStart. This will be updated as we iterate through each consecutive day.

Looping through the remaining days

Line 47 – Our next task is to iterate over the remaining days.

Line 50 – If a date is not a holiday or a weekend then we can subtract one from our remaining days.

Line 53 – If the day remaining then equals zero, we want to break from our loop and record the current day as our end day.

Line 56-57 – Upon each loop over the day except for the last target day, we need to add a day to our current day and then get the next day of the week.

Getting the end date and converting it to a date string.

The last thing we need to do on our map is to return the end date to our array of end dates.

Create and Publish Google Workspace Add-ons with Apps Script Course 300px

Line 60 – First we convert our day from UTC back into a JavaScript date.

Line 63 – Then we convert that date to a JavaScript ISO string which will display our our UTC date by year month and day in the format that we want. This will also display the time, which we don’t want. So we get the substring of this value by collecting the first 10 characters (YYYY-MM-DD).

Another example of ISO string data in action:

Create a ISO String from date text input intended for UTC date in JavaScript

Send it back.

Line 67 – Lastly, we send our mapped array of end dates back to runsies.

 

If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.

It’s always great to see how people use these tutorials and bits of code in their own projects. Feel free to share in the comments below.

~ Yagi

One thought on “Calculate the Workday End Date in JavaScript”

Leave a Reply