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

Home
Ruby
1.ActiveRecord
2.Array
3.CGI
4.Class
5.Collections
6.Database
7.Date
8.Design Patterns
9.Development
10.File Directory
11.GUI
12.Hash
13.Language Basics
14.Method
15.Network
16.Number
17.Rails
18.Range
19.Reflection
20.Statement
21.String
22.Threads
23.Time
24.Tk
25.Unit Test
26.Windows Platform
27.XML
Ruby » Time » Time Calculation 




Finding the Day of the Week


def every_sunday(d1, d2)
  one_day = d1.is_a?(Time60*60*24 1
  sunday = d1 + ((7-d1.wday7* 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(200611), Time.local(200624))

 














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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.