How to set the display time to Japan time in Rails

Thing you want to do

I want to change the posting time from international standard time to Japan time

Before change (GMT standard time) ▼ Screenshot from Gyazo After change (Japan time) ▼ Screenshot from Gyazo

How to set

--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

** 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>

How to define conversion methods

** 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

How to set the display time to Japan time in Rails
[Rails] How to display an image in the view
How to set chrony when the time shifts in CentOS7
[Rails] How to display information stored in the database in view
[Java] How to set the Date time to 00:00:00
Unify the Rails app time zone to Japan time
How to check Rails commands in the terminal
[Rails] How to convert UC time display to Japanese time display
[Rails 6] How to set a background image in Rails [CSS]
[rails] How to display db information
How to introduce jQuery in Rails 6
How to set Lombok in Eclipse
How to install Swiper in Rails
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] How to display the list of posts by category
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
How to implement search functionality in Rails
[Rails / Routing] How to refer to the controller in the directory you created
How to change app name in rails
How to insert a video in Rails
[Rails] How to use the map method
[rails] How to display parent information in child view in nested relationships
[Rails] How to display error messages individually
How to use MySQL in Rails tutorial
[Rails] How to omit the display of the character string of the link_to method
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
[Rails] How to use video_tag to display videos
How to use credentials.yml.enc introduced in Rails 5.2
[Rails 5] How to display the password change screen when using devise
How to display error messages in Japanese
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
[Rails] How to get the user information currently logged in with devise
How to compare only the time with Rails (from what time to what time, something like)
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to solve the time lag of created_at after save method
[Order method] Set the order of data in Rails
How to check the logs in the Docker container
I tried to organize the session in Rails
How to display a web page in Java
[Rails] How to use select boxes in Ransack
Set the time of LocalDateTime to a specific time
How to translate Rails into Japanese in general
[Java] (for MacOS) How to set the classpath
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
How to display the result of form input
How to build the simplest blockchain in Ruby
[Rails] How to use PostgreSQL in Vagrant environment
Spring Boot --How to set session timeout time
How to write Rails
How to uninstall Rails
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
[Rails] How to register multiple records in the intermediate table with many-to-many association
How to resolve errors that occur in the "Ruby on Rails" integration test
[Rails] How to operate the helper method used in the main application with Administrate