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
[Rails] How to convert UC time display to Japanese time display
[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.
Implementation policy to dynamically save and display Timezone in Rails
[Rails] How to define macros in Rspec and standardize processing
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
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
Things to remember and concepts in the Ruby on Rails tutorial
How to install Swiper in Rails
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
Change the save destination of the image to S3 in the Rails app. Part 2
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
[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
Change session timeout time in Spring Boot
How to display error messages in Japanese
[JSR-310 Date and Time API] Precautions for format definition when converting from Japanese calendar character string to date type
Let's create a TODO application in Java 9 Create TODO display Sort by date and time + Set due date default to today's date
A memo to simply create a form using only HTML and CSS in Rails 6
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)
Parse the date and time string formatted by the C asctime function in Java
Settings to display Japanese Javadoc in IntelliJ IDEA
Date and time acquisition method using DateAndTime API
[Validation] rails How to specify after today's date
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
[Japanese localization] i18n rails Easy Japanese localization view display only
How to implement a like feature in Rails
How to easily create a pull-down in Rails
Difference in date expression due to case inflection
How to use JQuery in js.erb of Rails6
How to build API with GraphQL and Rails
[Rails] Added in devise: username not added to database
[Rails] How to get success and error messages
Unify the Rails app time zone to Japan time
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Calendar class and Date class
How to implement a circular profile image in Rails using CarrierWave and R Magick