I want to change the posting time from international standard time to Japan time
Before change (GMT standard time) ▼
After change (Japan time) ▼
--Added the description of config.time_zone ='Tokyo'
in config / application.rb
application.rb
#↑ Codes before this are omitted
module App
class Application < Rails::Application
config.time_zone = 'Tokyo'
end
end
--If the server is up, shut it out and start it up again
docker-compose stop
docker-compose up -d
** This will be displayed in Japan time, but the format needs to be changed separately **
--Use the strftime method
to apply the method to the part where you want to change the display format.
erb:sample.html.erb
#Below is a sample
<td><%= @tweet.created_at.strftime('%Y year%m month%d day%H o'clock%M minutes') %></td>
** What to do: Define format conversion in Initialize **
--Create a file called time_formats.rb
under config / initializers
--Time :: DATE_FORMATS [: datetime_jp] ='% Y year% m month% d day% H hour% M minute'
time_formats.rb
Time::DATE_FORMATS[:datetime_jp] = '%Y year%m month%d day%H o'clock%M minutes'
--Write .to_s (: datetime_jp]) in the part you want to use (in the view file) and use it.
erb:sample.html.erb
#Below is a sample
<td><%= @tweet.created_at.to_s(:datetime_jp) %></td>
Recommended Posts