Datetime.jl icon indicating copy to clipboard operation
Datetime.jl copied to clipboard

Function to calculate business days between two dates

Open msravi opened this issue 12 years ago • 1 comments

It would be a nice enhancement to have a function that returns the number of business days between two dates, discounting Saturdays, Sundays, and holidays. Below is one such implementation that takes two dates and a list of holidays, and returns the number of business days between them:

function bizDays(frmDate, toDate, holidays=[])
  dbw = int(floor(float(toDate-frmDate)/7)*5 + (dayofweek(toDate)>=dayofweek(frmDate) ? (dayofweek(toDate)>5 ? 5 : dayofweek(toDate))-dayofweek(frmDate) : (5-(dayofweek(frmDate)>5 ? 5 : dayofweek(frmDate)))+dayofweek(toDate)))
  dbw -= length(find(h->(frmDate < h <= toDate), holidays))
  return dbw
end

The implementation assumes that the frmDate is always exclusive of the range and the toDate is always inclusive.

Regards, Ravi

Edit: Corrected typo in variable names

msravi avatar Jan 22 '14 19:01 msravi

This issue has been moved to the DateExtensions.jl package where it can be pursued there (a new package for experimental features).

https://github.com/quinnj/DateExtensions.jl/issues/1

quinnj avatar Aug 26 '14 01:08 quinnj