Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the current biweekly period in Rails

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?

like image 266
Jon Hester Avatar asked Dec 11 '25 12:12

Jon Hester


1 Answers

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).

like image 75
Daniel Vandersluis Avatar answered Dec 13 '25 01:12

Daniel Vandersluis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!