Change format of created_at in Rails 3.1?
Asked Answered
O

6

26

I am calling the date a record was created at in a basic app running Rails 3.1.

<%= @issue.created_at %>

The above outputs the following timestamp:

2011-09-10 14:44:24 UTC

What is the simplest way of altering the way this displays? I would like something like this:

10 Sept. 2011

and then somehow call it again with a different format:

14:44

so I can call it twice and merge the two together:

10 Sept. 2011
14:44

The reason I want to call it twice rather than create a helper to format a two line date/time is to allow me to call the date in some places and just the time in others.

Ore answered 10/9, 2011 at 15:5 Comment(0)
T
13

I would use I18n. Take a look at some example http://guides.rubyonrails.org/i18n.html#adding-date-time-formats. It's a clean and flexible way of formatting dates and times.

Tb answered 10/9, 2011 at 15:22 Comment(1)
Using the example in that link I now have <%= @issue.created_at, :format => :short %>, which results in an Exception error. Any ideas?Ore
D
77

The simplest thing to do is to use the strftime function

# Day / Month / Year
@issue.created_at.strftime("%d %b. %Y")
# Hour:Min
@issue.created_at.strftime("%H:%M")

You could put those two calls in separate helpers if you find yourself doing it a lot.

Duffey answered 10/9, 2011 at 15:19 Comment(3)
I get a no method error if I put those into the Issues helper file. Should I be altering the way I call created_at?Ore
Some may find this useful along with this solution: strftime docsCrenelation
@PeterPiper it depends on what you're doing, but you should also strongly consider https://mcmap.net/q/512560/-change-format-of-created_at-in-rails-3-1 in most cases.Duffey
T
13

I would use I18n. Take a look at some example http://guides.rubyonrails.org/i18n.html#adding-date-time-formats. It's a clean and flexible way of formatting dates and times.

Tb answered 10/9, 2011 at 15:22 Comment(1)
Using the example in that link I now have <%= @issue.created_at, :format => :short %>, which results in an Exception error. Any ideas?Ore
T
9
<%= l(@issue.created_at, :format=>:your_format) %>

in locale YAML (app/config/locale/country_id.yml) you must declare

time:
 formats:
  your_format: '%d %b. %Y'
  your_another_format: '%I:%M'

Date formatting should be declared inside your i18n YAML definition file for easy configure, and other date format could be found here

Taliesin answered 10/9, 2011 at 15:36 Comment(0)
I
7

Check out http://www.foragoodstrftime.com/ for an easy way to customize date/time formatting @spike

Isocracy answered 29/5, 2013 at 14:27 Comment(1)
Add Time::DATE_FORMATS[:default] = "%d/%m/%Y %H:%M" or some variation of this string it into a file in config/initializers (you could call it date_and_time.rb)Wheelbase
R
1

You can do:

@issue.created_at.to_date.iso8601

Rapper answered 5/1, 2017 at 21:10 Comment(0)
P
0

to_s also takes format input argument:

http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s

Providential answered 20/3, 2013 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.