Forum: Ruby on Rails date conversion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Justin H. (Guest)
on 2006-01-04 03:46
I have the date saved in a database in the following format
'20060102170020'. How do I go about converting the date in the format of
January, 01, 1901? I searched all over and found something similiar
using 'to_formatted_s'. However I only found support for converting
month to a three letter format (e.g. 'Jan'). Is there an eaiser way?
Thanks.
Justin H. (Guest)
on 2006-01-04 03:59
Justin Hernandez wrote:
> I have the date saved in a database in the following format
> '20060102170020'. How do I go about converting the date in the format of
> January, 01, 1901? I searched all over and found something similiar
> using 'to_formatted_s'. However I only found support for converting
> month to a three letter format (e.g. 'Jan'). Is there an eaiser way?
> Thanks.

I found the answer. '%B %d, %Y'.
Hasan D. (Guest)
on 2006-01-05 18:44
(Received via mailing list)
Juan:
On Jan 3, 2006, at 5:46 PM, Justin Hernandez wrote:

> I have the date saved in a database in the following format
> '20060102170020'. How do I go about converting the date in the
> format of
> January, 01, 1901? I searched all over and found something similiar
> using 'to_formatted_s'. However I only found support for converting
> month to a three letter format (e.g. 'Jan'). Is there an eaiser way?

Your column should be named <foo>_on or <foo>_at and be of the SQL
Date type. You can then use the strftime method to format the date as
you wish. As in:
model.bought_on.strftime("%B, %d, %Y") which will display the date in
the format you've requested.
Cheers,
Hasan D. <removed_email_address@domain.invalid>
Rick O. (Guest)
on 2006-01-05 18:44
(Received via mailing list)
> Your column should be named <foo>_on or <foo>_at and be of the SQL
> Date type. You can then use the strftime method to format the date as
> you wish. As in:
> model.bought_on.strftime("%B, %d, %Y") which will display the date in
> the format you've requested.
> Cheers,
> Hasan D. <removed_email_address@domain.invalid>

I have a post on defining named time formats.  This way you're not
littering your app with direct calls to #strftime.

http://rails.techno-weenie.net/tip/2005/11/20/defi...

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.