# Problem with date format

**URL:** <https://www.ruby-forum.com/t/problem-with-date-format/123139>\
**Category:** Rails\
**Created:** [December 14, 2007, 10:32am UTC](https://www.ruby-forum.com/t/problem-with-date-format/123139 "2007-12-14T10:32:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![veeraa2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@veeraa2003](https://www.ruby-forum.com/u/veeraa2003)\
**Post date:** [December 14, 2007, 10:32am UTC](https://www.ruby-forum.com/t/problem-with-date-format/123139/1 "2007-12-14T10:32:17Z")

</div>

Hi Everybody,

I have some issue with date formats.

Like I using calender select plugin to select date, It will return the  
date with US format like “MM-DD-YYYY”.

If I save this in mysql table, it stores like “YYYY-MM-DD”

12-31-2007 (31st Dec 2007) stored in MYSQL like 2007-31-12 ( Actually  
NULL cos 12 th day, 31 st month and 2007).

Like I cant store the date 12-31-2007 in US format in MYSQL table.

Any suggestion appreciated.

Regards,  
T.Veeraa.

---

<div class="post-metadata">

**Author:** ![veeraa2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@veeraa2003](https://www.ruby-forum.com/u/veeraa2003)\
**Post date:** [December 14, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/problem-with-date-format/123139/2 "2007-12-14T19:25:10Z")

</div>

On 12/14/07, Veera S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 12-31-2007 (31st Dec 2007) stored in MYSQL like 2007-31-12 ( Actually  
> NULL cos 12 th day, 31 st month and 2007).
> 
> Like I cant store the date 12-31-2007 in US format in MYSQL table.

Databases store dates in YYYY-MM-DD format so that operations like  
sorting by date work properly.

But why do you care. ActiveRecord will convert to and from ruby Time  
or DateTime objects and you can format them for display as you wish,  
no matter how they are stored in the DB.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![veeraa2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@veeraa2003](https://www.ruby-forum.com/u/veeraa2003)\
**Post date:** [December 14, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/problem-with-date-format/123139/3 "2007-12-14T20:42:36Z")

</div>

Lets try and make this easier for the OP to read…

You don’t need to worry how the date is stored in the tables. You are  
not going to access the tables directly (unless you need another  
external app interacting with the same database). You are going to  
access them trough Models.

When a model has a method that relates to a date column, it is going  
to return a Date object.

When a model has a method that relates to a datetime column, it is  
going to return a Time object.

So, lets say you have an Order model (that relates to the Orders  
table) and this Order model has a shipped\_at attribute (relates to the  
shipped\_at datetime column in your database). You can store the data  
on this attribute by using “@order.shipped\_at = Time.now”. If you need  
to read this datetime to show to the user in a view, you can use  
“@order.shipped\_at.to\_formatted\_s(:short)”. This will generate  
something readable.

For more help generating different Time objects, use:

[http://ruby-doc.org/core/classes/Time.html](http://ruby-doc.org/core/classes/Time.html)

Several examples are shown in AWDWR and on this group.
