# Formating date and time

**URL:** <https://www.ruby-forum.com/t/formating-date-and-time/173419>\
**Category:** Rails\
**Created:** [August 8, 2009, 11:45am UTC](https://www.ruby-forum.com/t/formating-date-and-time/173419 "2009-08-08T11:45:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aashishkiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@aashishkiran](https://www.ruby-forum.com/u/aashishkiran)\
**Post date:** [August 8, 2009, 11:45am UTC](https://www.ruby-forum.com/t/formating-date-and-time/173419/1 "2009-08-08T11:45:47Z")

</div>

I want to display date and time in view as “August 7, 2009 at 10:22:02  
am”. Presently it is displaying in view as “2009-08-07 10:22:02 UTC”.  
Can anyone tell to format date and time.

---

<div class="post-metadata">

**Author:** ![aashishkiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@aashishkiran](https://www.ruby-forum.com/u/aashishkiran)\
**Post date:** [August 8, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/formating-date-and-time/173419/2 "2009-08-08T12:20:49Z")

</div>

2009/8/8 Aashish K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I want to display date and time in view as Â “August 7, 2009 at 10:22:02  
> am”. Presently it is displaying in view as “2009-08-07 10:22:02 UTC”.  
> Can anyone tell to format date and time.

Have a look at strftime.  
(Did you try googling for ruby format date time, Google is usually  
quicker than posting here.).

Colin

---

<div class="post-metadata">

**Author:** ![aashishkiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@aashishkiran](https://www.ruby-forum.com/u/aashishkiran)\
**Post date:** [August 8, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/formating-date-and-time/173419/3 "2009-08-08T15:52:06Z")

</div>

THIS IS THE SOLUTION TRY THIS  
\<%=DateTime.now.strftime("%B %d %Y")%\> \<%= DateTime.now.strftime("%I:  
%M%P " ) %\>

---

<div class="post-metadata">

**Author:** ![aashishkiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@aashishkiran](https://www.ruby-forum.com/u/aashishkiran)\
**Post date:** [August 9, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/formating-date-and-time/173419/4 "2009-08-09T17:55:21Z")

</div>

If you want it formatted this way all the time, you might also want to  
consider setting a format in Time::DATE\_FORMATS via an initializer.

In your case, you’d drop this code into a file in config/initializers  
(say, date\_formats.rb):

Time::DATE\_FORMATS[:default] = “%A %d %Y at %H:%M:%S”

That should be close to what you want - look at the docs for strftime  
for more of the % symbols.

–Matt J.

On Aug 8, 5:45 am, Aashish K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
