# Date conversion

**URL:** <https://www.ruby-forum.com/t/date-conversion/51601>\
**Category:** Rails\
**Created:** [January 4, 2006, 2:46am UTC](https://www.ruby-forum.com/t/date-conversion/51601 "2006-01-04T02:46:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [January 4, 2006, 2:46am UTC](https://www.ruby-forum.com/t/date-conversion/51601/1 "2006-01-04T02:46:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [January 4, 2006, 2:59am UTC](https://www.ruby-forum.com/t/date-conversion/51601/2 "2006-01-04T02:59:30Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/date-conversion/51601/3 "2006-01-05T17:44:28Z")

</div>

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 \_on or \_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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/date-conversion/51601/4 "2006-01-05T17:44:46Z")

</div>

> Your column should be named \_on or \_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](mailto: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/defining\_custom\_date\_time\_formats](http://rails.techno-weenie.net/tip/2005/11/20/defining_custom_date_time_formats)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
