# Changing Time format globally

**URL:** <https://www.ruby-forum.com/t/changing-time-format-globally/105018>\
**Category:** Rails\
**Created:** [May 24, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018 "2007-05-24T22:24:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 24, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/1 "2007-05-24T22:24:18Z")

</div>

By default, whenever you stick a Time in a web page, e.g. \<%=  
created\_at %\>, it gets formatted as an unwieldy string: “Thu May 24  
21:19:11 BST 2007”

I’ve discovered that I can improve this using \<%= created\_at.to\_s(:db)  
%\> to get “2007-05-24 21:19:11”

However, is there a way to set the default conversion globally? Or do  
I have to find every template which includes a Time field and manually  
change it?

Thanks,

Brian.

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 24, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/2 "2007-05-24T23:11:26Z")

</div>

candlerb wrote:

> By default, whenever you stick a Time in a web page, e.g. \<%=  
> created\_at %\>, it gets formatted as an unwieldy string: “Thu May 24  
> 21:19:11 BST 2007”
> 
> I’ve discovered that I can improve this using \<%= created\_at.to\_s(:db)  
> %\> to get “2007-05-24 21:19:11”
> 
> However, is there a way to set the default conversion globally? Or do  
> I have to find every template which includes a Time field and manually  
> change it?

[http://www.ruby-forum.com/topic/57923](http://www.ruby-forum.com/topic/57923)

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 25, 2007, 12:13am UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/3 "2007-05-25T00:13:32Z")

</div>

Create a helper that parses the date and returns your desired format.  
Look  
up the documentation on helpers.

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 25, 2007, 7:44am UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/4 "2007-05-25T07:44:07Z")

</div>

> Create a helper that parses the date and returns your desired format.

That would mean I still had to modify every instance of a time  
variable in every template: e.g. changing them to  
\<%= time\_fmt(created\_at) %\>

But Jacob’s pointer for setting the :default time display looks to be  
what I want. Thank you!

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 25, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/5 "2007-05-25T16:02:02Z")

</div>

You could do this:  
Time::DATE\_FORMATS[:clean\_with\_time] = “%B %d, %Y at %I:%M %p”

inside either your environment.rb file(non rails edge) or inside a new  
file in the config/initializers directory (e.g. config/initializer/  
time.rb).

Then you can use that by doing:

created\_at.to\_s(:clean\_with\_time)

Robert

---

<div class="post-metadata">

**Author:** ![candlerb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@candlerb](https://www.ruby-forum.com/u/candlerb)\
**Post date:** [May 25, 2007, 7:56am UTC](https://www.ruby-forum.com/t/changing-time-format-globally/105018/6 "2007-05-25T07:56:09Z")

</div>

This screencast is all about it: [#31 Formatting Time - RailsCasts](http://railscasts.com/episodes/31)

Zach I.  
â†’ Blog – [http://www.zachinglis.com](http://www.zachinglis.com)  
â†’ Company – [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio – [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)
