Rajesh Gurbani
Rajesh Gurbani

Reputation: 211

How to use to_date in rails?

Ruby 2.0

controller action

@date = "2013-12-21"
@new_date = @date.to_date
p @new_date

o/p: Sat, 21 Dec 2013

at view

<%= @new_date %>

o/p: 2013-12-21

i want on view "Sat, 21 Dec 2013"

Upvotes: 1

Views: 3842

Answers (5)

Simone Carletti
Simone Carletti

Reputation: 176352

The best way to format a date object in Rails is by using the available I18n formatters.

<%= l @ new_date, format: :long %>

As described in the guide, you can also create additional formats. For example, if none of the defaults matches your desired format, simply define

# config/locales/en.yml
en:
  time:
    formats:
      custom: "%a, %e %b %Y"

then use

<%= l @ new_date, format: :custom %>

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118261

Rails is so intelligent that it automatically calls all objects in a view that are not already a string via the method .to_s, which always converts the content of the object to a string.

Lets first dig me into the root cause, why such unexpected output :

kirti@kirti-Aspire-5733Z:~/workspace/testproject$ rvm use 1.9.3
Using /home/kirti/.rvm/gems/ruby-1.9.3-p484
kirti@kirti-Aspire-5733Z:~/workspace/testproject$ rails c
Loading development environment (Rails 3.2.16)
1.9.3p484 :001 > d = "2013-12-21".to_date
 => Sat, 21 Dec 2013 
1.9.3p484 :002 > d.to_s
 => "2013-12-21" 
1.9.3p484 :003 > d
 => Sat, 21 Dec 2013 
1.9.3p484 :004 > d.class
 => Date 
1.9.3p484 :005 > d.strftime('%a, %e %b %Y')
 => "Sat, 21 Dec 2013" 
1.9.3p484 :006 > 

"2013-12-21".to_date giving you a Date instance, not a String instance.d.class proved that. d is a Date instance, on which in view again to_s method is called as I told in the begining, so Sat, 21 Dec 2013 is again set back into the "2013-12-21". So your solution will be :

@date = "2013-12-21".to_date.strftime('%a, %e %b %Y')

"2013-12-21".to_date.strftime('%a, %e %b %Y') will give you the desired result as an String instance.So on string instance(receiver) if you apply to_s,you will get the same receiver back. Now you can use this @date variable in your view.

Upvotes: 3

Niall Paterson
Niall Paterson

Reputation: 3580

From what I understand, you are getting

2013-12-21

but want

Sat, 21 Dec 2013

The reason it is outputted in the console nicely is because the console will auto format it for you. This can be verified by printing @new_date.to_s in the console, it'll be the unformatted version. To print it out on a screen in the way you want, you have to format it yourself.

You should use strftime like this:

@new_date = @new_date.strftime('%a, %e %b %Y')

or

@date.to_date.strftime('%a, %e %b %Y')

This will output Sat, 21 Dec 2013 correctly.

Upvotes: 1

Bharat soni
Bharat soni

Reputation: 2786

@new_date.strftime('%a, %e %b %Y')

Use it directly on the view.

Upvotes: 0

Bharat soni
Bharat soni

Reputation: 2786

in Ruby 2.0 in a controller's action:

@date = "2013-12-21"

@new_date = @date.to_date

# => Sat, 21 Dec 2013

And use the @new_date on your view.

Upvotes: 0

Related Questions