Created
April 16, 2018 05:53
Workdays in range minus vacation days
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
#!/usr/bin/env node | |
const mode = process.argv[2]; | |
const startDate = process.argv[3]; | |
const endDate = process.argv[4]; | |
const vacationDays = parseInt(process.argv[5], 10); | |
const sugar = require('sugar-date'); | |
const moment = require('moment'); | |
const parsedStartDate = moment(sugar.Date.create(startDate)); | |
const parsedEndDate = moment(sugar.Date.create(endDate)); | |
const holidays = [/*add your holidays here*/].map(d => moment(sugar.Date.create(d))); | |
function numberOfWorkDaysInInterval(parsedStartDate, parsedEndDate, holidays) { | |
if (parsedEndDate < parsedStartDate) { | |
throw new Error('invalid interval'); | |
} | |
// Loop every day between start and end | |
let days = 0; | |
for (let _date = parsedStartDate; _date < parsedEndDate; _date += (1000*60*60*24)) { | |
const date = new Date(_date); | |
if (date.getDay() !== 0 && date.getDay() !== 6) { | |
const holidayDiff = holidays.map(h => h.diff(date, 'seconds')).filter(diff => Math.abs(diff) === 0); | |
if (holidayDiff.length === 0) { | |
days++; | |
} | |
} | |
} | |
return days; | |
} | |
function numberOfWorkWeeksInInterval(parsedStartDate, parsedEndDate) { | |
if (parsedEndDate < parsedStartDate) { | |
throw new Error('invalid interval'); | |
} | |
// Loop every day between start and end | |
let weeks = 0; | |
for (let _date = parsedStartDate; _date < parsedEndDate; _date += (1000*60*60*24)) { | |
const date = new Date(_date); | |
if (date.getDay() === 0) { | |
weeks++; | |
} | |
} | |
return weeks; | |
} | |
if (mode === 'workdays') { | |
console.log(numberOfWorkDaysInInterval(parsedStartDate, parsedEndDate, holidays) - vacationDays); | |
} | |
if (mode === 'weeks') { | |
console.log(numberOfWorkWeeksInInterval(parsedStartDate, parsedEndDate)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment