From: Robert Klemme Date: 2011-01-11T17:49:55+09:00 Subject: Re: Ruby 1.9.2 Date issues On Tue, Jan 11, 2011 at 1:45 AM, keldog wrote: > Hello all... > > Wondering if someone can help me out with a date issue. > > My environment: > ruby: 1.9.2p0 > rails: 3.0.3 > mac os x > > My issue: > > From what I've been reading, the date format has been changed in > 1.9.2.  So, saving dates in the "mm/dd/yyy" format has been an issue > since 1.9.2 is saving as "dd/mm/yyyy" What exactly do you mean by "saving"? Both "mm/dd/yyy" and "dd/mm/yyyy" are just textual representations of a Date. If you want to be sure what conversion is used you can use Date#strftime: 09:44:35 ~$ irb19 -r date Ruby version 1.9.2 irb(main):001:0> d = Date.today => # irb(main):002:0> d.to_s => "2011-01-11" irb(main):003:0> d.strftime '%m/%d/%Y' => "01/11/2011" irb(main):004:0> d.strftime '%d/%m/%Y' => "11/01/2011" irb(main):005:0> If you save via Marshal there should be no issues. Note also that there are some built in formats: irb(main):012:0> d.rfc3339 => "2011-01-11" irb(main):013:0> d.rfc2822 => "Tue, 11 Jan 2011 00:00:00 +0000" irb(main):014:0> d.rfc822 => "Tue, 11 Jan 2011 00:00:00 +0000" > I have a jQuery plugin that uses the date picker and displays in the > format of "mm/dd/yyyy"  However, sometimes the date saves and other > times it doesn't because of ruby expecting the changed format (mm/dd/ > yyyy).  For example: > > 3/1/2011 saves as 1/3/2011 > 3/13/2011 doesn't save (returns "") I would expect the date picker to return a Date and not a String. What am I missing? > I have the following code in my model to inspect the date (in my case, > start_date of a course): > > [code] >  before_filter :fix_dates > >  private > >  def fix_dates >    raise self.start_date.to_s.inspect >  end > [/code] > > Tried the following: > 1) In the /config/initializers directory, created a date_format.rb to > change the date.  However, that's for display purposes only (I > believe). > > 2) In the fix_dates function above, I added: > self.start_date = Date.strptime(self.start_date.to_s,'%m/%d/%Y').to_s > But this isn't valid since ruby will set the date to an empty string > before executing this code. > > I think there is a really simple solution for this work around, but > I've failed to find one thus far.  Any help is greatly appreciated! Maybe you changed your environment and now a different default format for your locale is chosen. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/