9

I can get Rails to display mm/dd/yyyy using the following initializer:

date_format.rb

 Date::DATE_FORMATS[:default]="%m/%d/%Y"

or l18n using this answer

But this does not change the way dates are submitted. When submitted:

10/02/2014

Will be parsed as

 @entry.photo_date
 Mon, 10 Feb 2014

I'm looking for a way to submit a date in mm/dd/yyyy format.

I'd like to be able to do this centrally, so I do not have to repeat code throughout the application

smathy
  • 26,283
  • 5
  • 48
  • 68
jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36
  • @njzk2 i already saw that post, it shows how to format dates the way mine are formatted now, not how to format them in mm/dd/yyyy. – jamesdlivesinatree Jan 06 '15 at 19:20
  • and you can't adapt that answer to your format? – njzk2 Jan 06 '15 at 19:35
  • I've already tried, as you see above. @njzk2 – jamesdlivesinatree Jan 07 '15 at 00:03
  • Try using I18n, like [this answer](http://stackoverflow.com/a/5303521/1261474) suggests. – Adib Saad Jan 07 '15 at 04:35
  • I've updated my question. Linked answers work for displaying `%m/%d/%Y` correctly, but date submission still parses in the manner noted in my question. I read all of the answers and couldn't find it, or am I missing something? – jamesdlivesinatree Jan 07 '15 at 05:45
  • @jamesdlivesinatree: ok, so the issue is with date typed as text and submitted. I think the specific format here would vary depending on the locale of the client, wouldn't it? I think it would help if you detailed how the date is inputed. – njzk2 Jan 07 '15 at 14:22
  • @njzk2 As of now, it is a standard input box. As in, the client is instructed to write the date as `mm/dd/yyyy`. When the user inputs, it is parsed as `dd/mm/yyyy`. I have modified the l18n like you suggested, maybe there's a way to dictate this within there? – jamesdlivesinatree Jan 07 '15 at 18:58
  • 4
    Why is this question marked as duplicate? The linked question is about displaying dates, not accepting them as input. @jamesdlivesinatree, did you ever figure this out? – SteveO7 Aug 15 '15 at 19:14
  • Reopened and answered the actual question, also changed the title to make it clearer that you were talking about submitting dates, not displaying them. – smathy Apr 04 '18 at 23:14

2 Answers2

5

In modern ruby (ie. with prepend) you can insert your own type casting in front of Rails's. You'll want to do this for whatever other date/time formats you're using. Here's the code for Date, just stick this in an config/initializers/typecasts.rb or somewhere:

module Typecasting
  module Date
    def cast_value v
      ::Date.strptime v, "%m/%d/%Y" rescue super
    end  
  end  

  ::ActiveRecord::Type::Date.prepend Date
end

Rails will try the American format and fall back to using the builtin method if that didn't work.

smathy
  • 26,283
  • 5
  • 48
  • 68
2

Try this:

irb(main):001:0> today = Date.today
=> Tue, 06 Jan 2015
irb(main):003:0> today.strftime("%m/%d/%Y")
=> "01/06/2015"

So in your case, @entry.photo_date.strftime("%m/%d/%Y") should work.

neo
  • 4,078
  • 4
  • 25
  • 41