Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import moment from 'moment'
- /**
- * @param {number} workingDays - to subtract
- * @param {date|moment} [date] - date of reference
- * @return {date} result - of subtracting workingDays from date
- */
- export const subtractWorkingDays = (workingDays, date = moment()) => {
- const m = moment(date)
- // if we have more than 5 days to reduce, subtract as much weeks
- // as we can, asuming a week contains 5 business days
- if (workingDays > 5) {
- // ~~ is faster than parseInt or Math.floor
- m.subtract(~~(workingDays/5), 'week')
- workingDays = workingDays % 5
- }
- if (workingDays - (m.get('day') - 1/*monday*/) > 0) workingDays = workingDays + 2
- return m.subtract(workingDays, 'day').toDate()
- }
Add Comment
Please, Sign In to add comment