Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change default date format in ruby on rails?

I want to change default date format in Rails. Format should be y/m/d . I add following code into my environment.rb

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

But it didn't work. How can I change default format? I use rails 2.3.8 version

like image 701
Rosh Avatar asked Aug 22 '11 09:08

Rosh


People also ask

How do I change the date format in Ruby on Rails?

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.

How do I change the default date format in laravel?

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.


1 Answers

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"
like image 183
dexter Avatar answered Sep 20 '22 22:09

dexter