JavaScript automatically fill in weekdays for a specified number of days

Looking for assistance with a JavaScript problem. I have created functions that generate dates based on user input of a start date, but now I need to ensure that only working days (excluding weekends and holidays) are populated. How can I modify my code to accommodate this?

Current code snippet:

function GetModifiedDate(date, noOfDaysAdded) {
        var inputstring = date;
        var dString = inputstring.split('/');
        var dt = new Date(dString[2], dString[0] - 1, dString[1]);
        dt.setDate(dt.getDate() + parseInt(noOfDaysAdded));
        var finaldate = (String(dt.getMonth() + 1)) + '/' + (String(dt.getDate())) + '/' + (String(dt.getFullYear()));
        return finaldate;
    }
function AutoPopulateDatesForLndMgmtCust(obj) {
                var val = obj.value;
                var table = $('#tblLndMgmtCust');
                var rowLength = table[0].rows.length;
                var modifieddate = val;

                     incrementDay = [2, 1, 3, 1, 5, 2, 10, 4, 10, 2, 2, 2, 1, 1];

                for (var i = 0; i < rowLength - 2; i += 1) {
                    var row = table[0].rows[i + 2];
                    var cell = row.cells[2];

                    modifieddate = GetModifiedDate(modifieddate, incrementDay[i] * 1);
                    cell.children[0].children[0].value = modifieddate;
                }
            }

Thank you in advance!

Answer №1

let currentDate = new Date(2022, 11, 31, 12, 0, 0, 0);

let dayOfWeek = currentDate.getDay();
let isWeekendDay = (dayOfWeek == 6) || (dayOfWeek == 0);    // 6 = Saturday, 0 = Sunday

let workDay = currentDate.getDay();
let isWorkingDay = (workDay != 6) && (workDay != 0);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Building a dynamic button in React without relying on hardcoded properties

I'm currently working on creating a button in a React application, and here is the code I have so far: var React = require('react'); var buttonStyle = { margin: '10px 10px 10px 0' }; var Button = React.createClass({ render: ...

Load the file's contents into an array within a Vue component

https://i.sstatic.net/gxcYi.png My media.txt file contains the following URLs: "https://www.dropbox.com/s/******/687.jpg?dl=0", "https://www.dropbox.com/s/******/0688.jpg?dl=0 Incorporating a Vue carousel component: <template> <section> ...

Animate the leftward disappearance of a div to make it vanish

My goal is to create a user interface where users can navigate through different divs. Here is the HTML code: <article id="realize" class="realizeBox"> <div class="shown"> <div class="heading"> <h2>Realisati ...

Tips for determining the starting horizontal and vertical rotation of a camera for OrbitControls to utilize (Azimuthal and Polar Angles)

Is there a way to set the initial horizontal and vertical rotation of a PerspectiveCamera that the OrbitControl can then use? I attempted to use .rotateX(X) .rotateY(Y) in PerspectiveCamera, but it doesn't seem to have an effect. In the three.js docu ...

"Angularjs feature where a select option is left blank as a placeholder, pointing users

Currently, I am working with AngularJS (version < 1.4). When using ng-repeat in select-option, I encounter an extra blank option which is typical in AngularJS. However, selecting this blank option automatically picks the next available option. In my sce ...

Is there a way to slow down the falling effect on my navigation bar?

As I was working on my personal website, I had a creative idea to add a falling-down animated effect instead of keeping the layout fixed. Utilizing bootstrap for navigation, I encountered some difficulty in controlling the speed of the falling effect. Any ...

Issue with label alignment in Chart.js after updating through ajax call

Utilizing chart.js allows me to showcase real-time data... var initiateAJAX = function() { $.ajax({ url: url, success: function(){ var currentTime = ++time var currentValue = Math.random()*1000; ...

Node.js - Node Mailer: Invalid Credentials for Username and Password

Encountering Error with Nodemailer and Gmail SMTP: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials v185sm51735194pfb.14 - gsmtp Securing Email Credentials in S ...

How do I use jQuery to make a div appear when the previous one closes?

I've been experimenting with some code and it's gotten pretty lengthy. It works fine with just a few divs, but what if I need to implement this on 20 or more...? Is there a way to condense the code I've written? (I'm still new to jque ...

Is there a way to verify duplicate email addresses without the need to click any button?

As I work on developing a web application, I am faced with the challenge of checking for duplicate email addresses in real-time without the need to press a button. This check must be done by comparing the data with information stored in the database. Since ...

The functionality of fullcalendar is running smoothly on the local environment, however, it is encountering

My calendar is designed to showcase events pulled from a database. When testing it on localhost, everything ran smoothly. However, upon uploading the calendar, the events failed to display. ...

Out of nowhere, I started getting a 401 error from .net and jquery

My ajax call used to work perfectly: $J.ajax({ type: 'POST', url: "/ajax/getCharts", data: JSON.stringify(cids), dataType: 'json', asynch: false }) However, things changed yesterday. We implemented Microsoft.AspNet ...

Creating string enums in NextJS with TypeScript

I am working on my nextjs application and I have a component with a prop that is a string. I decided to create an enum, so I attempted the following: enum Label { dermatology = 'Dermatologi', psychology = 'Psykologi', rheumatology = ...

Click on an image to dismiss a material-ui dialog

Trying to include a clickable image to close a material-ui dialog, but encountering an issue where the onClick event is not responding. The props.onRequestClose function works fine when clicking outside of the dialog. What could be causing this problem? ...

Blend multiple images using Angular

Is there a way to combine multiple images in Angular? I came across some HTML5 code that seemed like it could do the trick, but unfortunately, I couldn't make it work. <canvas id="canvas"></canvas> <script type="text/javascript"> ...

Steps for altering the inner HTML of a div

i currently have a div and an HTML editor that I am attempting to work with: add the content from the div into the editor. (completed) then save any changes made in the editor back into the div. below is the code snippet: protected void Page_Load(objec ...

Changing array indices in JavaScript by splicing elements

I'm experiencing a curious issue that seems to involve overwriting array indices after splicing, or at least that's what I suspect. This problem arises in a small game project built using phaser 2 - essentially, it's a multiplayer jumping ga ...

Is it possible to conduct a feature test to verify the limitations of HTML5 audio on

Is there a way to detect if volume control of HTML5 audio elements is possible on iOS devices? I want to remove UI elements related to the volume if it's not alterable. After referring to: http://developer.apple.com/library/safari/#documentation/Aud ...

Using the Repeater Control as a parameter for commanding a nested gridview

I'm facing an issue with a repeater containing a nested gridview. I need to retrieve a value databound in a label within the repeater and use it as an input parameter for the gridview. Below is some sample code to provide better clarity on my problem ...

Assign the value in the text box to the PHP session variable

Currently, I am developing a PHP "interactive text game" as a part of my assignment project. My task involves creating a user interface where users input information (such as character names) into text boxes that appear sequentially as they complete the p ...