Finding the Day of the Week : Time Calculation « Time « Ruby






Finding the Day of the Week


def every_sunday(d1, d2)
  one_day = d1.is_a?(Time) ? 60*60*24 : 1
  sunday = d1 + ((7-d1.wday) % 7) * one_day
  while sunday < d2
    yield sunday
    sunday += one_day * 7
  end
end

def print_every_sunday(d1, d2)
  every_sunday(d1, d2) { |sunday| puts sunday.strftime("%x")}
end

print_every_sunday(Time.local(2006, 1, 1), Time.local(2006, 2, 4))

 








Related examples in the same category

1.You can see the difference in two Time objects by subtracting them:
2.Add seconds and multiples of seconds to add to a time with +
3.Doing Time Arithmetic
4.Is it a leap year