0 notes &
Rails - core extensions - Date
Have you ever thought about how would be living in the future? It’s difficult to predict what’s going to happen and I think everyone is a bit curious to know how the earth will be in 300 years… Well, maybe you prefer to think about the past instead of the future. Sometimes I get myself thinking about how nice would be to live 500 years ago, but every time that I think about it, I remember they didn’t have antibiotics nor painkillers, so I force myself to think about something else.
Even if you have never thought about future or past, sometimes we have to deal with them. I mean, in the code. Sometimes we have to figure out if something happened in the past or if it’s going to happen in the future and using the same explanation I did in my previous post Rails - core extensions - Array, I decided to talk about Date and how Rails extends it to help us to have a better life.
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
My grandmother always said: “The use of a time travel machine can make you dizzy” and I have to agree with her. Perhaps you are looking for a simple way to change the date. Let’s say you have an specific date and you want to change its year. 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.
I hope this post can help you to have a great time when working with Rails and dates. Don’t forget, this is a limited list of extensions that Rails does to the Date class, based on the methods that I am using more often. Rails defines more extensions to Date class and you can check all of them at http://api.rubyonrails.org/classes/Date.html. It’s also available in the active_support code at: activesupport-3.1.3/lib/active_support/core_ext/date file.






