Formated date/time

Have some way to I get a formated date/time on rails?

I know I can get date/time by timestamps

created_at :datetime
updated_at :datetime

Bu, I can get with an formated way when I call the date/time on a block?

like php for example (“M”, “D”, “Y”) and not the complete information
“2000-01-01
14:26:00 UTC”

Thanks!


Fernando A.

On 7 June 2011 16:25, Fernando A. [email protected]
wrote:

Have some way to I get a formated date/time on rails?
I know I can get date/time by timestamps
created_at :datetime
updated_at :datetime
Bu, I can get with an formated way when I call the date/time on a block?
like php for example (“M”, “D”, “Y”) and not the complete information
“2000-01-01 14:26:00 UTC”

You can use strftime to format a date/time

Colin

I think what you want is date.strftime("%d/%m/%Y")

2011/6/7 Colin L. [email protected]

You can use strftime to format a date/time
http://groups.google.com/group/rubyonrails-talk?hl=en.


Carina B. Silva
http://twitter.com/carinabs8
http://carinabs8.wordpress.com/
“…Estou disposto a demolir paredes,contruir pontes e acender
fogueiras.
Tenho uma grande experincia,um monte de energia,um pouco dessa coisa de
‘viso’ e no tenho medo de comear do zero.”-Steve Jobs

You can do a lot of things with dates. You can create a date.rb file in
your initializers and do something like this:

Time::DATE_FORMATS.merge!(

:complete => ‘%B %d, %Y %I:%M %p’,
:abbrev_date => ‘%b %d, %Y’,
:normal_date => ‘%B %d, %Y’,
:normaltime => ‘%I:%M %p’,
:abbrev_complete => ‘%b. %d, %Y %I:%M %p’

)

%a - The abbreviated weekday name (``Sun’’)

%A - The full weekday name (``Sunday’’)

%b - The abbreviated month name (``Jan’’)

%B - The full month name (``January’’)

%c - The preferred local date and time representation

%d - Day of the month (01…31)

%H - Hour of the day, 24-hour clock (00…23)

%I - Hour of the day, 12-hour clock (01…12)

%j - Day of the year (001…366)

%m - Month of the year (01…12)

%M - Minute of the hour (00…59)

%p - Meridian indicator (AM'' orPM’’)

%S - Second of the minute (00…60)

%U - Week number of the current year,

starting with the first Sunday as the first

day of the first week (00…53)

%W - Week number of the current year,

starting with the first Monday as the first

day of the first week (00…53)

%w - Day of the week (Sunday is 0, 0…6)

%x - Preferred representation for the date alone, no time

%X - Preferred representation for the time alone, no date

%y - Year without a century (00…99)

%Y - Year with century

%Z - Time zone name

%% - Literal ``%’’ character

Then call Time.now.to_s(:abbrev_date) or similar.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs