Override Default Date Format in Rails Admin
Table of contents
Rails Admin is a Rails engine that provides an easy-to-use interface for managing your data. It’s perfect for the cases where we want admin dashboard quickly for CRUD (Create, Read, Update and Delete) operations.
When using engines, it can be difficult to override its default behavior. It was the same case for overriding the default date format. It was tricky as I didn’t know exactly where to look at.
After some research, I found out that Rails Admin uses long date and time format from the locale. We can check the related code in official gem repository.
Line for the exact code may change in the future, if it has, you can search for the code below:
register_instance_option :date_format do
:long
end
Override default date format
To override the default format of the date and display the format we want in our UI, we will need to add the required format in our locale files so the values inside the engine are overridden.
Add the following to config/locale/en.yml
```rb
en:
date:
formats:
long: "%Y-%m-%d"
time:
formats:
long: "%Y-%m-%d %H:%M:%S"
```
Please change format of the date and time as required for your application.
If you have other locales that your Rails app supports, you can update the date formats as required in related locale file by copying this exact code and updating content inside key “long”
NOTE: date is for datatype date and time is for data type datetime
Conclusion
If you restart the rails server and reload the UI, you should be able to see the date format you added.
Thanks for reading. Happy coding!
Image Credits
- Cover Image by Estée Janssens on Unsplash