# TimeRange 'this weekend'

**URL:** <https://www.ruby-forum.com/t/timerange-this-weekend/138032>\
**Category:** Rails\
**Created:** [May 19, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032 "2008-05-19T17:27:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [May 19, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032/1 "2008-05-19T17:27:05Z")

</div>

How would one calculate ‘this weekend’?

In my event model I have:

def self.dates\_for\_time\_range(range)  
case range  
when ‘today’ then [Date.today, Date.today]  
when ‘tomorrow’ then [Date.tomorrow, Date.tomorrow]  
when ‘this\_week’ then [Date.today, 1.week.from\_now.to\_date]  
else raise ArgumentError, “Incorrect time range: #{range}”  
end  
end

Which works perfectly, but trying to figure out what ‘this weekend’  
has proven to be a bit more difficult.

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [May 19, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032/2 "2008-05-19T18:25:18Z")

</div>

I’m sure Fred will have a better way of attacking this but something  
like this should work:

[(1.week.from\_now.beginning\_of\_week-2.days).to\_date,  
1.week.from\_now.beginning\_of\_week.to\_date]

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [May 19, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032/3 "2008-05-19T18:36:31Z")

</div>

Thanks alot! This works exactly right. I modified the -2 to -3 to  
include Friday. This is actually very close to what I had tried, but I  
did not have the parenthesis correct. Too bad I wasted a whole day  
before trying the forum.

Best regards,

Kyle

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [May 19, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032/4 "2008-05-19T18:40:41Z")

</div>

On 19 May 2008, at 17:24, AndyV wrote:

> I’m sure Fred will have a better way of attacking this but something  
> like this should work:
> 
> [(1.week.from\_now.beginning\_of\_week-2.days).to\_date,  
> 1.week.from\_now.beginning\_of\_week.to\_date]

Well there’s the slightly shorter  
[Date::today.monday + 5, Date::today.monday + 7]  
(and you don’t have to worry about whether that will do funny things  
when the clocks go back and what not).

Fred

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [May 19, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/timerange-this-weekend/138032/5 "2008-05-19T22:25:53Z")

</div>

Probably overkill for this simple question but chronic so cool, I  
thought I would mention it. Chronic is an incredible time parsing  
library that makes parsing natural languages into time a sinch. ‘next  
weekend’ is only the tip of the iceberg.

[http://chronic.rubyforge.org/](http://chronic.rubyforge.org/)

Chronic.parse(‘tomorrow’)  
#=\> Mon Aug 28 12:00:00 PDT 2006

Chronic.parse(‘monday’, :context =\> :past)  
#=\> Mon Aug 21 12:00:00 PDT 2006
