JSFiddle - React, Tailwind, and code Playground

by Vinay Pratap Singh Bhadauria

JavaScript

$(document).ready(function () {
    alert("ready");
    function GetNextworkingDay() {
        //get the value of Start Date here
        var startDate = new Date(); // this gets the current date// can also provide any specific date here.
        var endDate = new Date();
        //Get the No of Days to perform a task.
        var NoOfDaysForTask = 3;
        //Initial End Date
        endDate.setDate(startDate.getDate() + NoOfDaysForTask);
        endDate.setHours(00, 00, 00);
        var WorkingDaysCountInRange = parseInt(GetWorkingDaysCountInRange(startDate, endDate))
        //Increase the range and calculate till we get the Specified no foworking days.
        while (NoOfDaysForTask != WorkingDaysCountInRange) {
            endDate.setDate(endDate.getDate() + parseInt(NoOfDaysForTask - WorkingDaysCountInRange));
            WorkingDaysCountInRange = parseInt(GetWorkingDaysCountInRange(startDate, endDate))
        }
        alert(endDate);
    });

//This function will check if we are getting the required no of workingdays in the range limit passed
//if not then range will be Increased and it will be calculated again
function GetWorkingDaysCountInRange(startDate, endDate) {
    var taskStartDate = new Date(startDate);
    var taskCompletionDate = new Date(endDate);
    var weekDaysCount = 0
    while (taskStartDate < taskCompletionDate) {
        var day = taskStartDate.getDay();
        var isWeekend = (day == 6) || (day == 0);
        if (!isWeekend) {
            weekDaysCount++;
        }
        taskStartDate.setDate(taskStartDate.getDate() + 1);
    }
    var day = taskCompletionDate.getDay();
    var isWeekend = (day == 6) || (day == 0);
    if (isWeekend) {
        weekDaysCount--;
    }
    return weekDaysCount;
}
})