Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 578 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rails date format in form field

#1
I'd like my dates in the mm/dd/year format in text fields. However, they currently displays as 2010-03-26.

Is there a global setting I can set to change this?

I tried the following, which seems to update the .to_s method, but form fields stay the same.

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y')

Thanks
Reply

#2
The `date_select` form helper provides a "bare bones" date selector.

Reply

#3
I know this is an awfully old question, but you could use `date_field` instead of `text_field`. Perhaps that wasn't an option when you asked this question originally.

It displays the date in `mm/dd/yyyy`, which is your intent.

`<%= date_field :column_name %>`

Reply

#4
You have to register the `default` format in an initializer.

Add this line to the `config/initializers/date_time_formats.rb`.

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

# if you want to change the format of Time display then add the line below
Time::DATE_FORMATS[:default]= '%m/%d/%Y %H:%M:%S'

# if you want to change the DB date format.
Time::DATE_FORMATS[:db]= '%m/%d/%Y %H:%M:%S'

Now in the `script\console` lets test the format.

>> Date.today.to_s
=> "03/14/2010"

>> Time.now.to_s
=> "03/14/2010 13:20:55"

Reply

#5
I don't know if there is a global setting for that anywhere, I just do it in the ERB.

<%= text_field_tag("air_date_date", air_date.blank? ? "" : air_date.strftime("%m/%d/%Y"), :class => "date-input text") %>

Alternatively, you can factor this out into a helper function to make it DRY.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through