Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript date calculations ignore weekends

function getBusinessDateCount (startDate, endDate) {
    var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
    var ifThen = function (a, b, c) {
        return a == b ? c : a;
    };

    elapsed = endDate - startDate;
    elapsed /= 86400000;

    daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;
    daysAfterLastSunday = endDate.getDay();

    elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
    elapsed = (elapsed / 7) * 5;
    elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);

    return Math.ceil(elapsed);
}

function calc() {
  let start = document.querySelector('#startDate').value,
      end = document.querySelector('#endDate').value,
      result = getBusinessDateCount(new Date(start), new Date(end));
  document.querySelector('#result').value = result;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript sistemas 
Javascript :: check if word has accented or unaccented javascript 
Javascript :: redblobgames pathfinding 
Javascript :: atom javascript es6 linter 
Javascript :: deploy angular app on google app engine 
Javascript :: função que retorna uma media aritmética javascript 
Javascript :: conditional rendering alert if input fields are empty 
Javascript :: regular expression arabic and persion 
Javascript :: replace array element javascript stack overflow 
Javascript :: adding a variable to a string without using + in javascript 
Javascript :: adjust() js 
Javascript :: node code comment process real time 
Javascript :: demo.pesapal.com api keys stackoverflow 
Javascript :: select coordinates of image 
Javascript :: hide url in discord.js 
Javascript :: mixin in javascript mdn 
Javascript :: keyboard is underlined in eclipse javascript 
Javascript :: Amazon VPC supporting 5 different IP address ranges and i wanted to know how wide those rangers are 
Javascript :: in object transform translate property concat with rotate value angular 7 
Javascript :: 5.625/2 
Javascript :: javascript picture delete after time 
Javascript :: function resizeBase64Img(base64, newWidth, newHeight) { return new Promise<string((resolve, reject)={ 
Javascript :: javascript alert program 
Javascript :: jquery user function override 
Javascript :: how to filter data from mongodb date in reactjs 
Javascript :: chrome back button click event react 
Javascript :: class in side class in jss 
Javascript :: vuetifyjs 2.0 2 column side bar 
Javascript :: ref={(node) = (this.avatarEditor = node)} 
Javascript :: chart js bars too light 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =