JavaScript get working days

Examples

Get the day of the week:

const d = new Date();
let day = d.getDay();

Try it Yourself »

Get the day of the week of a specific date:

const d = new Date("July 21, 1983 01:15:00");
let day = d.getDay();

Try it Yourself »

More examples below.


Definition and Usage

The getDay() method returns the day of the week (0 to 6) of a date.

Sunday = 0, Monday = 1, ... (See below):


Syntax

Parameters

Return Value

TypeDescriptionA numberThe day of the week (0 to 6).

More Examples

Get the name of the weekday (not just a number):

const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const d = new Date();
let day = weekday[d.getDay()];

Try it Yourself »

Browser Support

getDay() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

ChromeEdgeFirefoxSafariOperaIEYesYesYesYesYesYes

The problem is that that does not give you an accurate count - you also need to allow for holidays, and working those out is a lot harder, and will vary from region to region.

Add your solution here

 B   I   U   S  small BIG code var  <   >   &  link [^] encode untab case indent outdent

Preview 0

Existing Members

...or Join us

Download, Vote, Comment, Publish.

Your Email  

This email is in use. Do you need your password?

Optional Password  

When answering a question please:

  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Sometimes when thinking about something I want to post, a particular part of it grabs my attention and I decide to rip it out and write something focused on just that one aspect. That's what happened today when I was thinking about a particular way of doing date math and I wanted to see if it would make sense.

Given a particular date, I want to add a certain number of days to it (or weeks, or months), but I want the result to "prefer" business days. This is not the same as adding business days. So for example, 20 days from now is pretty different from 20 business days from now. Rather, I want the main difference to be close to my desired duration, but just "adjusted" to fall on a business day.

To test out my theory that this was possible, I decided to use the date-fns date library.

JavaScript get working days

date-fns has a wealth of useful date utilities, including a couple I'd need to implement my logic:

  • add - lets you quickly add durations to dates
  • isWeekend - returns true if a date falls on a weekends
  • nextMonday - fires up a Web3 crypto miner via Docker and Lambda to generate machine-driven art. No, actually it just returns the next Monday after a date

I began with a simple function that takes an input date and a duration in days:

const addDayPreferBusinessDay = (date,numDays) => {
    let newDate = add(date, { days:numDays });
    if(isWeekend(newDate)) {
        newDate = nextMonday(newDate);
    }
    return newDate;
}

Pretty straightforward I think. I wrote a quick test that started with today and added 0 to 14 days. Here's how the results looked:

Fri 2022-09-23, plus 0 days, using addDayPreferBusinessDay: Fri 2022-09-23
Fri 2022-09-23, plus 1 days, using addDayPreferBusinessDay: Mon 2022-09-26
Fri 2022-09-23, plus 2 days, using addDayPreferBusinessDay: Mon 2022-09-26
Fri 2022-09-23, plus 3 days, using addDayPreferBusinessDay: Mon 2022-09-26
Fri 2022-09-23, plus 4 days, using addDayPreferBusinessDay: Tue 2022-09-27
Fri 2022-09-23, plus 5 days, using addDayPreferBusinessDay: Wed 2022-09-28
Fri 2022-09-23, plus 6 days, using addDayPreferBusinessDay: Thu 2022-09-29
Fri 2022-09-23, plus 7 days, using addDayPreferBusinessDay: Fri 2022-09-30
Fri 2022-09-23, plus 8 days, using addDayPreferBusinessDay: Mon 2022-10-03
Fri 2022-09-23, plus 9 days, using addDayPreferBusinessDay: Mon 2022-10-03
Fri 2022-09-23, plus 10 days, using addDayPreferBusinessDay: Mon 2022-10-03
Fri 2022-09-23, plus 11 days, using addDayPreferBusinessDay: Tue 2022-10-04
Fri 2022-09-23, plus 12 days, using addDayPreferBusinessDay: Wed 2022-10-05
Fri 2022-09-23, plus 13 days, using addDayPreferBusinessDay: Thu 2022-10-06
Fri 2022-09-23, plus 14 days, using addDayPreferBusinessDay: Fri 2022-10-07

Given that today is Friday, you can see how the next few days all bump to Monday, and do so again next week.

For the heck of it, I decided to compare this to strictly adding business days. date-fns has a function for this, ]addBusinessDays](https://date-fns.org/v2.29.3/docs/addBusinessDays). Here's how those results look:

Fri 2022-09-23, plus 0 days, using addBusinessDays: Fri 2022-09-23
Fri 2022-09-23, plus 1 days, using addBusinessDays: Mon 2022-09-26
Fri 2022-09-23, plus 2 days, using addBusinessDays: Tue 2022-09-27
Fri 2022-09-23, plus 3 days, using addBusinessDays: Wed 2022-09-28
Fri 2022-09-23, plus 4 days, using addBusinessDays: Thu 2022-09-29
Fri 2022-09-23, plus 5 days, using addBusinessDays: Fri 2022-09-30
Fri 2022-09-23, plus 6 days, using addBusinessDays: Mon 2022-10-03
Fri 2022-09-23, plus 7 days, using addBusinessDays: Tue 2022-10-04
Fri 2022-09-23, plus 8 days, using addBusinessDays: Wed 2022-10-05
Fri 2022-09-23, plus 9 days, using addBusinessDays: Thu 2022-10-06
Fri 2022-09-23, plus 10 days, using addBusinessDays: Fri 2022-10-07
Fri 2022-09-23, plus 11 days, using addBusinessDays: Mon 2022-10-10
Fri 2022-09-23, plus 12 days, using addBusinessDays: Tue 2022-10-11
Fri 2022-09-23, plus 13 days, using addBusinessDays: Wed 2022-10-12
Fri 2022-09-23, plus 14 days, using addBusinessDays: Thu 2022-10-13

I think you can really see, especially at the end, how things begin to diverge. Is my way "better"? For what I want to use it for, I think it is. Obviously, time will tell, and like always, I'd love to know if people think this is useful. Note I used another useful date-fns utility to render my dates, format

How to find working days in JavaScript?

JS: Calc business days Monday to Friday var startDay = startDate. getDay(); var endDay = endDate. getDay();

How to get business days in JavaScript?

Date. workingDaysFrom(fromDate) calculates the number of working days between 2 Date objects (excluding weekends; Sat and Sun). The method will return “-1” if the fromDate is an invalid Date object or is later than the compared Date.

How to get weekdays in JavaScript?

JavaScript - Date getDay() Method Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

How can I calculate working days?

To calculate the number of working days between two dates, we must follow these steps:.
Count the number of days between the two dates..
Subtract the number of weekend days between the two dates..