Change date and time to Japanese notation in Rails

Introduction

You can use the table column created_at to retrieve the date and time when the data was created. However, it is difficult to use it as it is.

This time, I would like to be able to retrieve it with a notation such as 2020/7/21 9:05:50.

Display the creation date and time

First, let's extract the created_at of the data @ post in the posts table as it is.

erb:posts/show.html.erb


<p>Event: <%= @post.event %></p>
<p>place: <%= @post.place %></p>
<p>Creation date and time: <%= @post.created_at %></p>
place event created_at
Tokyo Fireworks display 2020-07-21 00:05:50 UTC

You can see that it is very different from the target 2020/7/21 9:05:50 notation.

Specifically, the following three points are different.

  1. ʻUTC` (World Standard Time)
  2. / is replaced with -
  3. The notation of 7 is 07

I would like to correct it in order.

1. Change from World Standard Time to Japan Time

Since programming is used all over the world, it will be output in World Standard Time as it is. Therefore, let's change it to Japan time.

config/application.rb


class Application < Rails::Application
  # *****Add the following line*****
  config.time_zone = "Asia/Tokyo"
end

By inserting this one line, I was able to change World Standard Time to Japan Time as shown below.

place event created_at
Tokyo Fireworks display 2020-07-21 09:05:50 +0900

There is a +0900, which means that the time difference between Japan time and World Standard Time is +9 hours.

2. Change the format

It's Japan time, but it's hard to understand if the format is 2020-07-21 09:05:50 + 0900. Let's change it.

First, install the gem for multilingual support and set it to Japanese.

Gemfile


#For Rails6
gem 'rails-i18n', '~> 6.0'
#For Rails5
gem 'rails-i18n', '~> 5.1'

Terminal


bundle install

config/application.rb


class Application < Rails::Application
  #Abbreviation
  config.time_zone = "Asia/Tokyo"
  # *****Add the following line*****
  config.i18n.default_locale = :ja
end

Then use the l method provided by rails-i18n.

erb:posts/show.html.erb


<p>Event: <%= @post.event %></p>
<p>place: <%= @post.place %></p>
<!-- *****add l***** -->
<p>Creation date and time: <%= l @post.created_at %></p>

In addition, let's create a file called config / locales / ja.yml and set the format.

config/locales/ja.yml


ja:
  time:
    formats:
      default: "%Y/%m/%d %H:%M:%S"

As a result, the notation changes as follows, and +0900 also disappears.

place event created_at
Tokyo Fireworks display 2020/07/21 09:05:50

The format can be changed freely, for example, if you don't need seconds, you can omit:% S.

config/locales/ja.yml


      default: "%Y/%m/%d %H:%M"
place event created_at
Tokyo Fireworks display 2020/07/21 09:05
Symbol Meaning
% Y Year
% m Month (MONTH)
% d DAY
% H HOUR
% M Minutes (MINUTE)
% S seconds (SECOND)

[Reference page] Ruby 2.7.0 Reference Manual (strftime)

3. Eliminate zero (0) padding

For example, it is unnatural for July to be written as 07, so let's remove 0.

config/locales/ja.yml


ja:
  time:
    formats:
      # ***** %Behind-Add*****
      default: "%Y/%-m/%-d %-H:%M:%S"
place event created_at
Tokyo Fireworks display 2020/7/21 9:05:50

By inserting -, we were able to eliminate 0 padding as described above.

Recommended Posts

Change date and time to Japanese notation in Rails
[Ruby on Rails] Change the update date and creation date to your favorite notation
[Rails] How to write in Japanese
How to change app name in rails
Date and time
How to translate Rails into Japanese in general
[Rails] How to convert UC time display to Japanese time display
How to set the display time to Japan time in Rails
[Ruby on Rails] How to write enum in Japanese
[Rails] Precautions when comparing date and time with DateTime
How to write a date comparison search in Rails
Usability of date and time classes in each language
Handling of date and time in Ruby. Use Date and Time properly.
How to change application.properties settings at boot time in Spring boot
[Java] Change language and locale to English in JVM options
Implementation policy to dynamically save and display Timezone in Rails
[Rails] How to define macros in Rspec and standardize processing
Sample code to parse date and time with Java SimpleDateFormat
[Rails] Search method when date and time have each column
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
Get the current date and time by specifying the time zone in Thymeleaf
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Rails] How to introduce kaminari with Slim and change the design
[Rails] Validation settings and Japanese localization
How to delete large amounts of data in Rails and concerns
[MySQL] [java] Receive date and time
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
How to introduce jQuery in Rails 6
Things to remember and concepts in the Ruby on Rails tutorial
How to install Swiper in Rails
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
Use Timecop in Rails web app to travel time on your browser
[Java] How to get the current date and time and specify the display format
How to change the maximum and maximum number of POST data in Spark
(For beginners) [Rails] Time saving tech! How to install and use slim
[Rails] How to create a table, add a column, and change the column type
Change the save destination of the image to S3 in the Rails app. Part 2
How to implement search functionality in Rails
How to implement date calculation in Java
How to get date data in Ruby
How to use custom helpers in rails
Install Webpacker and Yarn to run Rails
Explanation of Ruby Time and Date objects
How to use MySQL in Rails tutorial
Change List <Optional <T >> to Optional <List <T >> in Java
[Java] How to set the Date time to 00:00:00
How to fix system date in JUnit
Java 8 to start now ~ Date time API ~
How to build SquashTM and how to support Japanese
Steps to set a favicon in Rails
[Rails] [Memo] When to add = to <%%> and when not
[rails] How to configure routing in resources
Notation to put a variable in a string
[Ruby on Rails] Japanese notation of errors
How to get the date in java
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
Java to C and C to Java in Android Studio