I'm working on an application (training class app) that currently saves and displays the training class start date time as UTC.
Migration
class CreateTrainingClasses < ActiveRecord::Migration[5.2]
def change
create_table :training_classes do |t|
t.datetime :starts_at
end
end
end
Date Time Select

Date Time Display

However, I want to save and display local time (CST). I've updated the date time select form field to display the desired local time.
<%= form.label :starts_at %>
<p><%= form.datetime_select :starts_at, default: Time.now.in_time_zone("Central Time (US & Canada)"), ampm: true %></p>

And I've updated the display to show local time. However, it is now showing the wrong time—even when I create a new training class.
<td><%= training_class.starts_at.in_time_zone("Central Time (US & Canada)").strftime("%B %d, %Y %I:%M %p") %></td>

How do I save and display the correct local time for both the form and the class display?
You need to set time_zone in application.rb
config.time_zone = 'Central Time (US & Canada)'
Then restart server
Reference: https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
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