Change Default Date Format in Ruby on Rails

setting default date format in rails

If you want to use localization you have to express that in your view with the l helper method, like:

 <%= f.text_field :start_date, :value => l(@job.start_date.to_date) %>

Only now will Rails try to format the value with your localization settings. It is using the formatting as defined for Time, since :start_date probably is of type DateTime.

The format definitions in config/locales/en.yml should be sufficient.

Changing the default date and time format in Rails 4

Found a nice approach through the Rails Internationalization (I18n) API

Data and time formats can be 'translated' by adding the format to the i18n configuration.

config/locales/en.yml

en:
date:
formats:
default: "%d/%m/%Y"
time:
formats:
default: "%d/%m/%Y %H:%M"

Note: remember to not have tabs for the indent, like I did first time :)


As mentioned by NoelProf in the comments

To use i18n conversion don't forget the l (lower case L) before your
date in views! For example: <%= l your_date %>

You are invited to comment if you found other ways working well.

How do I set a default date format in Rails

# config/initializers/date_formats.rb
Date::DATE_FORMATS[:default] = "%m/%d/%Y"

change default date format in ruby on rails?

Add a file to config/initializers with the following code:

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

Rails ignores the default date format after upgrading from 6.1 to 7.0

This feature was deprecated in Rails 7. You can turn it back on with config.active_support.disable_to_s_conversion

Deprecate passing a format to #to_s in favor of #to_fs in Array, Range, Date, DateTime, Time, BigDecimal, Float and, Integer.

This deprecation is to allow Rails application to take advantage of a Ruby 3.1 optimization that makes interpolation of some types of objects faster.

New applications will not have the #to_s method overridden on those classes, existing applications can use config.active_support.disable_to_s_conversion.

Default Date Format in Rails (Need it to be ddmmyyyy)

In your settings file: config/environment.rb"

my_date_formats = { :default => '%d/%m/%Y' } 

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_date_formats)

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_date_formats)

source:
http://thedevelopercorner.blogspot.com/2009/03/change-default-date-format-in-ruby-on.html

How to change date format from mm/dd/yyyy to dd/mm/yyyy Ruby on Rails

Two steps:

  • You need to convert your string into Date object. For that, use Date#strptime.
  • You can use Date#strftime to convert the Date object into preferred format.

See implementation below:

str = '01/14/2018'

date = Date.strptime(str, '%m/%d/%Y')
=> #<Date: 2018-01-14 ((2458133j,0s,0n),+0s,2299161j)>

date.strftime('%d-%m-%Y')
=> "14-01-2018"

date.strftime('%Y-%m-%d')
=> "2018-01-14"

how can i change default date input in rails form

use

  <div class="field columns large-3">
<%= form.label :planned_start_date %>
<%= form.date_select :planned_start_date, order: [:day, :month, :year], class: 'select-date' %>
</div>

<div class="field columns large-3">
<%= form.label :planned_end_date %>
<%= form.date_select :planned_end_date, order: [:day, :month, :year], class: 'select-date' %>
</div>

API Documentation for order option reference can be found here

Change Date default to_s format in the scope of library

Create another module with a refinement to Date::to_s

module MyDate
refine Date do
def to_s
# here goes your implementation of to_s
end
end
end

And then:

 class NeedsCustomDateFormat
using MyDate
# All Date instances will have the custom to_s
end


Related Topics



Leave a reply



Submit