Sneha Kachroo
Sneha Kachroo

Reputation: 593

Proper date time format in rails

In my application I want to display the date in this format: 21st Aug,2012 07:30 PM. I have done it using strftime like this:

<%= @project.planned_start_date.strftime("%d %B %Y") if @project.planned_start_date-%>

It is displaying like this: 02 August 2012 Can someone help me out how to get the above mentioned format of date and time ?

Upvotes: 0

Views: 1871

Answers (4)

FedeX
FedeX

Reputation: 446

Have you tried to format your time value using the following strftime options?:

<%= @project.planned_start_date.strftime("%d %b, %Y %I:%M %p") unless @project.planned_start_date.blank? %>

EDIT: This is what you need then

<%= ActiveSupport::Inflector.ordinalize(@project.planned_start_date.strftime("%d").to_i) + @project.planned_start_date.strftime(" %b, %Y %I:%M %p") unless @project.planned_start_date.blank? %>

OUTPUT: "23rd Aug, 2012 09:19 AM"

Upvotes: 1

Brian
Brian

Reputation: 6840

>> t = Time.now
=> Tue Aug 21 08:59:54 -0400 2012
>> t.strftime("#{t.day.ordinalize} %B, %Y %I:%M %p")
=> 21st August, 2012 08:59 AM

Upvotes: 1

praveen k
praveen k

Reputation: 65

You can try this.

 <%= @project.planned_start_date.strftime("%d %b, %Y %I:%M %p") if @project.planned_start_date.present? -%>

output: 21 Aug,2012 07:30 PM

Upvotes: 1

Peter Brown
Peter Brown

Reputation: 51697

Checkout the strftime method, this has all the possible conversion characters and what they represent.

Upvotes: 1

Related Questions