Monday 11 February 2013

Change date format in ruby on rails

Suppose you have date data type and you want to change the format of date being saved from 'yyyy-mm-dd' to 'dd-mm-yyyy' or in any other format then do the following:

Add a file time_defaults.rb in initializers folder and add the  following code


# Default format for displaying dates and times
Date::DATE_FORMATS[:default] = "%d/%m/%Y"
Time::DATE_FORMATS[:default] = "%d/%m/%Y"


And restart the server.

No comments:

Post a Comment