I want to run some Find statements that only show results from the current two week period of a company - it's the pay period for the company. In other words, not necessarily just the last two weeks of records. A quick search looks like I am not going to be able to use a built-in Time method like Time.now.beginning_of_quarter
or Time.now.end_of_quarter
. What's the best way to implement this?
views:
62answers:
1
+1
A:
If you use the Date
class instead of Time
, you could make use of the cweek
method to get which week of the year a date falls into, and divide that by two to determine which biweekly period.
For instance:
now = Date.today #=> Thu, 10 Dec 2009
period = now.cweek / 2 #=> 25
Note that cweek takes the fact that the year doesn't divide equally into 52 weeks (and therefore not into 26 periods either).
Daniel Vandersluis
2009-12-10 20:23:41
Perfect. That's exactly what I needed. Thank you!
JetShred
2009-12-10 20:33:04