Working with Dates
in
Ruby on Rails
Nick Nguyen
@thenicknguyen
Ruby Date/Time
$ irb
> Date.new => #<Date: -4712-01-01 ((0j,0s,0n),+0s,2299161j)>
> Time.new => 2013-11-10 20:43:21 -0600
> DateTime.new
=> #<DateTime: -4712-01-01T00:00:00+00:00 ((0j,0s,0n),+0s,2299161j)>
Strftime
> Date.today.strftime("%B %d, %Y")
=> "November 11, 2013"
> Date.today.strftime("%m/%d/%y")
=> "11/11/13"
> DateTime.now.strftime("%H:%M")
=> "13:06"
Date.parse
> Date.parse("2013-11-11")
=> #<Date: 2013-11-11 ...>
> Date.parse("11/11/2013")
=> #<Date: 2013-11-11 ...>
> Date.parse("11-11-13") => #<Date: 2011-11-13 ((2455818j,0s,0n),+0s,2299161j)>
# OOPS!
Strptime
> Date.strptime("2013-11-11", "%Y-%m-%d")
=> #<Date: 2013-11-11 ... >
> Date.strptime("11/11/2013", "%m/%d/%Y")
=> #<Date: 2013-11-11 ... >
> Date.strptime("11-11-13", "%m-%d-%y")
=> #<Date: 2013-11-11 ... >
Rails Date/Time
$ rails c
> Date.new => Mon, 01 Jan -4712
> Time.new => 2013-11-10 21:02:13 -0600
> DateTime.new => Mon, 01 Jan -4712 00:00:00 +0000
What's up with that?
ActiveSupport
Provides Ruby language extensions and utilities.
http://guides.rubyonrails.org/active_support_core_extensions.html
https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext
Displaying Dates/Times
# open_camp/app/views/tasks/_task.html.erb
<div class="span2"> <%= task.due_date %> </div>
Displaying Dates/Times
Change Date Format
# open_camp/app/views/tasks/_task.html.erb
<div class="span2"> <%= task.due_date.strftime("%b %d, %Y") %>
</div>
Change Date Format
Define Custom Format
via Localization
# config/locales/en.yml
en:
hello: "Hello world" date: formats: awesometown: "%b %d, %y"
# app/views/tasks/_task.html.erb
<div class="span2"> <%= l task.due_date, :format => :awesometown %> </div>
Define Custom Format
Define Custom Format
via Initializer
2.0.0p247 :025 > Date::DATE_FORMATS
=> {
:short=>"%e %b",
:long=>"%B %e, %Y",
:db=>"%Y-%m-%d",
:number=>"%Y%m%d", :long_ordinal=>#<Proc:0x007f8d7aa31100@/Users/nick/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.13/lib/active_support/core_ext/date/conversions.rb:12 (lambda)>, :rfc822=>"%e %b %Y"}
# config/initializers/date_formats.rb
Date::DATE_FORMATS[:awesometown] = "%b %d, %y"
# app/views/tasks/_task.html.erb
<div class="span2"> <%= task.due_date.to_s(:awesometown) %> </div>
Set as Default
# config/initializers/date_formats.rb
Date::DATE_FORMATS[:default] = "%b %d, %y"
# app/views/tasks/_task.html.erb
<div class="span2"> <%= task.due_date %> </div>
Extensions to Date/DateTime
yesterday tomorrow beginning_of_week (at_beginning_of_week) end_of_week (at_end_of_week) monday sunday weeks_ago prev_week (last_week) next_week months_ago months_since beginning_of_month (at_beginning_of_month) end_of_month (at_end_of_month) prev_month (last_month) next_month beginning_of_quarter (at_beginning_of_quarter) end_of_quarter (at_end_of_quarter) beginning_of_year (at_beginning_of_year) end_of_year (at_end_of_year) years_ago years_since prev_year (last_year) next_year |
$ irb
> Date.today.tomorrow => NoMethodError: undefined method `tomorrow' for #<Date: 2013-11-11 ...
> Date.today.next_week
=> NoMethodError: undefined method `next_week' for #<Date: 2013-11-11 ...
> Date.today.beginning_of_year
=> NoMethodError: undefined method `beginning_of_year' for #<Date: 2013-11-11 ...
$ rails c
> Date.today.tomorrow => Tue, 12 Nov 2013
> Date.today.next_week => Mon, 18 Nov 2013
> Date.today.beginning_of_year => Tue, 01 Jan 2013
Open Camp Exercise
Add the ability to repeat a task daily, weekly, monthly until a specified date.
Use ActiveSupport extensions to make your life easier and code easier to understand.
Working with Dates in Rails
By Nick Nguyen
Working with Dates in Rails
- 2,647