Guest User

Untitled

a guest
Oct 17th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import moment from 'moment'
  2.  
  3. /**
  4. * @param {number} workingDays - to subtract
  5. * @param {date|moment} [date] - date of reference
  6. * @return {date} result - of subtracting workingDays from date
  7. */
  8. export const subtractWorkingDays = (workingDays, date = moment()) => {
  9. const m = moment(date)
  10. // if we have more than 5 days to reduce, subtract as much weeks
  11. // as we can, asuming a week contains 5 business days
  12. if (workingDays > 5) {
  13. // ~~ is faster than parseInt or Math.floor
  14. m.subtract(~~(workingDays/5), 'week')
  15. workingDays = workingDays % 5
  16. }
  17. if (workingDays - (m.get('day') - 1/*monday*/) > 0) workingDays = workingDays + 2
  18. return m.subtract(workingDays, 'day').toDate()
  19. }
Add Comment
Please, Sign In to add comment