user1575148 user1575148 - 4 years ago 86
Ruby Question

Given a date, how to find if it's the second or fourth Saturday of the month

In my Rails application, I need to find if a given date is the second or fourth Saturday of the month. What's the efficient way to do this? Is there a gem I can use?

Answer Source

This checks if the day is a saturday and the zero-based week number is odd (1 or 3 in our case) :

def second_or_fourth_saturday?(date)
  date.saturday? && (( - 1) / 7).odd?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download