[RUBY] 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
How to display the text entered in text_area in Rails with line breaks
[Rails 6] How to set a background image in Rails [CSS]
[rails] How to display db information
[Rails] How to write in Japanese
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 use custom helpers 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
Steps to set a favicon in Rails
[Behavior confirmed in December 2020] How to implement the alert display function
[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
[Rails] How to reset the database in production environment (Capistrano version)
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 write the view when Vue is introduced in Rails?
How to display error messages in Japanese
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
[Java] How to get the current date and time and specify the display format
[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
[Swift] How to set an image in the background without using UIImageView.
[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
[Rails] How to decide the destination by "rails routes"
[Order method] Set the order of data in Rails
How to check the logs in the Docker container
[Ruby on Rails] How to display error messages
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
How to prevent direct URL typing in Rails
How to add sound in the app (swift)
[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