Calculate workdays in a date range


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # Calculate the number of weekdays between dates
  2. workdays = 0
  3. date_has_weekend = false
  4. date = self.start_at
  5. while date.to_date < finish_at.to_date
  6. #If we have a weekend day, increase finish date
  7. if date.to_date.wday == 6 or date.to_date.wday == 0
  8. date_has_weekend = true
  9. finish_at = finish_at.to_time.advance(:days => 1)
  10. end
  11. workdays = workdays + 1 unless date.to_date.wday == 0 or date.to_date.wday == 6
  12. date = date.to_time.advance(:days => 1)
  13. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.