Last active
October 1, 2021 09:27
-
-
Save icemilo/a0b98a1508aab82853eb to your computer and use it in GitHub Desktop.
Calculates business days between two dates using moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require('moment'); | |
function calculateBusinessDays(firstDate, secondDate){ | |
//Initiallize variables | |
var day1 = moment(firstDate); | |
var day2 = moment(secondDate); | |
var adjust = 0; | |
if((day1.dayOfYear() === day2.dayOfYear()) && (day1.year() === day2.year())){ | |
return 0; | |
} | |
//Check if second date is before first date to switch | |
if(day2.isBefore(day1)){ | |
day2 = moment(firstDate); | |
day1 = moment(secondDate); | |
} | |
//Check if first date starts on weekends | |
if(day1.day() === 6) { //Saturday | |
//Move date to next week monday | |
day1.day(8); | |
} else if(day1.day() === 0) { //Sunday | |
//Move date to current week monday | |
day1.day(1); | |
} | |
//Check if second date starts on weekends | |
if(day2.day() === 6) { //Saturday | |
//Move date to current week friday | |
day2.day(5); | |
} else if(day2.day() === 0) { //Sunday | |
//Move date to previous week friday | |
day2.day(-2); | |
} | |
var day1Week = day1.week(); | |
var day2Week = day2.week(); | |
//Check if two dates are in different week of the year | |
if(day1Week !== day2Week){ | |
//Check if second date's year is different from first date's year | |
if (day2Week < day1Week){ | |
day2Week += day1Week; | |
} | |
//Calculate adjust value to be substracted from difference between two dates | |
adjust = -2 * (day2Week - day1Week); | |
} | |
return day2.diff(day1, 'days') + adjust; | |
} |
I believe the function should:
- include start and end date (so today "minus" yesterday equals two, given both are business days)
- to avoid side affects of user input, we should use start of the day (I just have the cast that I was testing this function at 23:50 and as New Date() creates Date at 01:00 AM, the difference was rounded to zero.
Here is my solution in typescript:
calculateBusinessDays(firstDate, secondDate){
// EDIT : use of startOf
let day1 = moment(firstDate).startOf('day');
let day2 = moment(secondDate).startOf('day');
// EDIT : start at 1
let adjust = 1;
if((day1.dayOfYear() === day2.dayOfYear()) && (day1.year() === day2.year())){
return 0;
}
if(day2.isBefore(day1)){
const temp = day1;
day1 = day2;
day2 = temp;
}
//Check if first date starts on weekends
if(day1.day() === 6) { //Saturday
//Move date to next week monday
day1.day(8);
} else if(day1.day() === 0) { //Sunday
//Move date to current week monday
day1.day(1);
}
//Check if second date starts on weekends
if(day2.day() === 6) { //Saturday
//Move date to current week friday
day2.day(5);
} else if(day2.day() === 0) { //Sunday
//Move date to previous week friday
day2.day(-2);
}
const day1Week = day1.week();
let day2Week = day2.week();
//Check if two dates are in different week of the year
if(day1Week !== day2Week){
//Check if second date's year is different from first date's year
if (day2Week < day1Week){
day2Week += day1Week;
}
//Calculate adjust value to be substracted from difference between two dates
// EDIT: add rather than assign (+= rather than =)
adjust += -2 * (day2Week - day1Week);
}
return day2.diff(day1, 'days') + adjust;
}
This is super handy, thanks!!
Error when year is changed.
Eg: day1 = 1575381077000; Tuesday, December 3, 2019
day2= 1578059477000; Friday, January 3, 2020
response is 30; which is wrong
Thanks bro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i fixed it using:
var diff=moment.duration(day2.diff(day1));
var diffIndays= diff.asDays()
console.log("DiffIndays: "+diffIndays)
//check if both dates lies on weekends
if ((day1.day() === 6 || day1.day() === 0) && (day2.day() === 6 || day2.day() === 0) && diffIndays < 2){
return 0
}
Please verify if this is correct