Rails 3 mysql date

Hello,

I have a problem with date when saving into my sql with rails 3.

Into my environment.rb file i put Date::DATE_FORMATS[:default] =
"%d/%m/%Y"

i created a jquery calendar and into the text field the date is for
example 01/10/2010.
When i save, the date saved is 10/01/2010. the monthes and the days are
inversed.

into the log parameter is : "birthday"=>"01/10/2010"

and the update (db) : UPDATE `profiles` SET `birthday` = '2010-01-10'
...

format is saved as MM/DD/YYYY

Does anyone know how to make this work ?

Thank you.

Yannick Yanikos wrote:

Hello,

I have a problem with date when saving into my sql with rails 3.

Into my environment.rb file i put Date::DATE_FORMATS[:default] =
"%d/%m/%Y"

i created a jquery calendar and into the text field the date is for
example 01/10/2010.
When i save, the date saved is 10/01/2010. the monthes and the days are
inversed.

into the log parameter is : "birthday"=>"01/10/2010"

and the update (db) : UPDATE `profiles` SET `birthday` = '2010-01-10'
...

format is saved as MM/DD/YYYY

Does anyone know how to make this work ?

Thank you.

I have also seen this problem. The DATE_FORMATS line seems to only tell
Rails that you want dates displayed this way. Mysql does whatever it
wants and ignores the instructions. There are a few ways that claim to
change this behavior on the web, but none have worked for me yet. I'd
love to see an answer to this problem. The inversed date is because
dates are shown that way in Europe and that is the way Mysql sees them.

Bob <bsm2th@gmail.com>

Yannick Yanikos wrote:

Hello,

I have a problem with date when saving into my sql with rails 3.

Into my environment.rb file i put Date::DATE_FORMATS[:default] =
"%d/%m/%Y"

i created a jquery calendar and into the text field the date is for
example 01/10/2010.

Is this supposed to be 1 October or 10 January?

When i save, the date saved is 10/01/2010. the monthes and the days are
inversed.

into the log parameter is : "birthday"=>"01/10/2010"

and the update (db) : UPDATE `profiles` SET `birthday` = '2010-01-10'
...

format is saved as MM/DD/YYYY

...so it's saving to the DB as 10 January. Is that what you want, or do
you want 1 October?

Does anyone know how to make this work ?

Thank you.

Best,

Marnen Laibow-Koser wrote:

Yannick Yanikos wrote:

Hello,

I have a problem with date when saving into my sql with rails 3.

Into my environment.rb file i put Date::DATE_FORMATS[:default] =
"%d/%m/%Y"

i created a jquery calendar and into the text field the date is for
example 01/10/2010.

Is this supposed to be 1 October or 10 January?

When i save, the date saved is 10/01/2010. the monthes and the days are
inversed.

into the log parameter is : "birthday"=>"01/10/2010"

and the update (db) : UPDATE `profiles` SET `birthday` = '2010-01-10'
...

format is saved as MM/DD/YYYY

...so it's saving to the DB as 10 January. Is that what you want, or do
you want 1 October?

Does anyone know how to make this work ?

Thank you.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

It seems to save correctly, just the display is wrong. In the US,
1/10/69 should be jan 10,1969, but after the save, it displays as
10/1/69

Bob

Yannick Yanikos wrote:

Hello,

I have a problem with date when saving into my sql with rails 3.

Into my environment.rb file i put Date::DATE_FORMATS[:default] =
"%d/%m/%Y"

i created a jquery calendar and into the text field the date is for
example 01/10/2010.
When i save, the date saved is 10/01/2010. the monthes and the days are
inversed.

into the log parameter is : "birthday"=>"01/10/2010"

and the update (db) : UPDATE `profiles` SET `birthday` = '2010-01-10'
...

format is saved as MM/DD/YYYY

Does anyone know how to make this work ?

Thank you.

I have encountered the same issue using Ruby 1.9.2. However, when I
switch back to Ruby 1.8.7, the issue is no longer present. What version
of Ruby are you using? If you use 1.8.7, do you experience the same
behavior?

I am running into the same issue. I have confirmed it works properly
on rails 3 and ree 1.8.7, but not on rails 3, ruby 1.9.2 (I am
wanting US formatted date strings)

[ruby-1.9.2] rails console
Loading development environment (Rails 3.0.0)

Offer.last.update_attributes(:publish_start => '31/10/10')

=> true

Offer.last.update_attributes(:publish_start => '10/31/10')

=> false

exit

[ree-1.8.7] rails console
Loading development environment (Rails 3.0.0)

Offer.last.update_attributes(:publish_start => '31/10/10')

=> false

Offer.last.update_attributes(:publish_start => '10/31/10')

=> true

It appears this is the issue.

http://slightlycoded.com/blog/ruby-1-9-date-problems

What is the preferred way to fix this in ruby 1.9 and rails?

Found a plugin which aims to address localization and delocalization,
but may be useful for this problem:

http://github.com/clemens/delocalize

You can try to personalize you jQuery datepicker as follows:

[code]
$("#project_formatted_start_date").datepicker({ altField:
'#project_formatted_start_date',altFormat: 'yy-mm-dd'});
[/code]
See jQuery API for datepicker for more details.

This has to do with how ruby 1.9.x changed the default Date.parse

see this sample script:

require 'time'
require 'date'

r = Date.parse("10/1/2010")

puts r.strftime("%a %d %B %Y")

ruby-1.9.2-p0 => Sun 10 January 2010
ruby-1.8.7-p302 => Fri 01 October 2010

One solution for Ruby 1.9.2 would be to redefine Date.parse as:

class Date
  def self.parse(input)
    Date.strptime(input, "%m/%d/%Y")
  end
end

You might also want to look at http://github.com/clemens/delocalize