I was looking for a way to change the default date format in Rails 4.
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.
The default date format shows as YYYY-MM-DD.
Note that the default date format is YYYY-MM-DD; therefore, if your string is of different format you must incorporate the format argument. There are multiple formats that dates can be in; for a complete list of formatting code options in R type ? strftime in your console.
So, let's add these variables to config/app. return [ 'date_format' => 'm/d/Y', 'date_format_javascript' => 'MM/DD/YYYY', ]; And then, in Laravel, we can use this: Carbon::createFromFormat(config('app. date_format'), $value)->format('Y-m-d'); Carbon::parse($value)->format(config('app.
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.
Add this
# Date Date::DATE_FORMATS[:default] = "%d/%m/%Y" # Time Time::DATE_FORMATS[:default] = "%d/%m/%Y %H:%M"
to config/initializers/date_time.rb
Then restart the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With