ExamplesGet the day of the week: Show const d = new Date(); Get the day of the week of a specific date: const d = new Date("July 21, 1983 01:15:00"); More examples below. Definition and UsageThe Sunday = 0, Monday = 1, ... (See below): SyntaxParametersReturn ValueTypeDescriptionA numberThe day of the week (0 to 6).More ExamplesGet the name of the weekday (not just a number): const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; const d = new Date(); Browser Support
ES1 (JavaScript 1997) is fully supported in all browsers: 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 Preview 0Existing Members...or Join usDownload, Vote, Comment, Publish. Your EmailThis email is in use. Do you need your password? When answering a question please:
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. date-fns has a wealth of useful date utilities, including a couple I'd need to implement my logic:
I began with a simple function that takes an input date and a duration in days:
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:
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:
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.. |