I have a column called test_date, type datetime in General Exam table. I built a form to create and update GeneralExam, on form, I allow User choose a date and time throught jQuery UI Datepicker.
After user choose date and time, it will displayed in textfield, like:
25-11-2012 16:30
It is my current time. When I run in console, I see the test_date was saved in database is:
2012-11-25 09:30:00
And when I get object GeneralExam, and get test_date value, it is:
g = GeneralExam.last
g.test_date
=> Sun, 25 Nov 2012 16:30:00 ICT +07:00
I think test_date is converted, because I set up in application.rb is:
config.time_zone = 'Hanoi'
But when I update a General Exam on form, it still display the time was saved in database, not my local timezone, it means it display:
2012-11-25 09:30:00
But I want it is like what I chose on form:
25-11-2012 16:30:00
How can I set up for display datetime column in local time?
And I think the order of date was reversed, when I choose date, it is 25-11-2012, but when it displayed it was 2012-11-25, Why it reversed the order?
After few hours I search on google and stackoverflow, I think I found what I want. I have looked at this question and find some information about
strftimemethod. This is what information I need to solve my problem.First, How to display datetime was saved in UTC as local time when display it:
To display value I want, I used this on form (I used simple_form):
I have got value in database and format it by:
And when I want display
test_datein table data, I also usedstrftime("%d-%m-%Y, %H:%M")to display datetime format as I want.