Rails - core extensions - Date

That's not a philosophical post, but sometimes we have to deal with the past and/or future. That's totally fine. Maybe you have to figure out if something happened in the past or if it’s going to happen in the future. Using the same approach of my previous post [POST], I would like to share the Rails extensions to Ruby built-in Date class I usually use when I am working.

today / tomorrow / yesterday

Today (the date this post was written) is Sat, 03 Mar 2012. And Rails give us a nice way to have this information, using the “today” method.

> Date.today
=> Sat, 03 Mar 2012

In the same way, we can also discover which day would be tomorrow or which day was yesterday, using “tomorrow” and “yesterday”:

> Date.tomorrow
=> Sun, 04 Mar 2012

> Date.yesterday
=> Fri, 02 Mar 2012

beginning_of… / end_of…

Which day was in the beginning of the X (where X could be: week, month, year)? You might also want to know which day will be in the end of X (the same X). We have a set of methods ready to help us out.

Using the same date

> today = Date.today
=> Sat, 03 Mar 2012

You can ask a date instance information about the beginning/ending of: week, month and year:

> today.beginning_of_week
=> Mon, 27 Feb 2012

> today.end_of_week
=> Sun, 04 Mar 2012

> today.beginning_of_month
=> Thu, 01 Mar 2012

> today.end_of_month
=> Sat, 31 Mar 2012

> today.beginning_of_year
=> Sun, 01 Jan 2012

> today.end_of_year
=> Mon, 31 Dec 2012

With the same name pattern, but with a slightly different result, Rails defines the methods “beginning_of_day” and “end_of_day”. It will return an ActiveSupport::TimeWithZone instance, instead of a Date, containing time details.

> today.beginning_of_day
=> Sat, 03 Mar 2012 00:00:00 UTC +00:00

> today.end_of_day
=> Sat, 03 Mar 2012 23:59:59 UTC +00:00

advance

What would be the first thing you would do if you had a time travel machine? I bet you are thinking about lots of things just right now, but before you continue, let me explain why I did this question. Rails defines a method that allow you travel to a specific date, just like a time travel machine. Rails defines it as “advance” and we are going to play with it.

Our initial date will be today:

> today
=> Sat, 03 Mar 2012

Let’s travel to the future, 1 year from now:

> today.advance(:years => 1)
=> Sun, 03 Mar 2013

Maybe one year is too much, let’s use 1 month instead:

> today.advance(:months => 1)
=> Tue, 03 Apr 2012

Well, you can define your own trip using different combinations of: years, months, weeks and days. It allows us to do things like:

> today.advance(:years => 42, :months => 42, :weeks => 42, :days => 42)
=> Mon, 04 Aug 2058

change

Perhaps you are looking for a simple way to change an existent date instance. For times like this, you have “change”:

> today
=> Sat, 03 Mar 2012

> today.change(:year => 2000)
=> Fri, 03 Mar 2000

You can change pretty much everything and have a complete new date.

This is a limited list of extensions to Ruby Date extensions provided by Rails. This list is based on my experience and needs using it. For a more detailed list, visit the Rails documentation site or browse its source code.

You might also like

SinatraSimpleRouter

I am open sourcing a project that I have been using a lot lately. You can easily define routes and some rules...

Comparison between Rack, Sinatra and Webmachine

Benchmarking Ruby and Erlang

Healthyr - Rails app performance monitor

Open sourcing a lib I created to benchmark Rails apps, using ActiveSupport::Notifications

Download free e-book

Learn different strategies on API versioning with "Versioned APIs with Phoenix" free e-book