[RAILS] Use the l method for time notation


When I was looking up to display the operating hours of the store, I found that I could specify the time notation by using something called strftime.


 <%= @laundry.open_time.strftime('%-H:%M')%> 〜 <%= @laundry.close_time.strftime('%-H:%M')%> 

Now you can write `07: 00 ~ 23: 00`. However, I was worried that `strftime ('% H:% M')` was repeated twice, and when I was investigating other methods, I found the l method (El method). Record the process of changing from the above writing style to the writing style with the l method.

Flow until using l method

1 Set the time zone (It was already set when strftime was used, but I will describe it so that I can look back in the future)
Before using the l method, it is necessary to set the time zone so that it is displayed in Japan time (if it is not set, it will be ```UTC (Coordinated Universal Time) `` `, and Japan There is a difference of 9 hours from the time)

config/application.Describe as follows in rb.

#### **`config/application.rb`**

 config.time_zone = 'Tokyo'

2 Change the default language to ja, create yml file
Set the language to Japanese by writing the following in the same file as 1.


config.i18n.default_locale = :ja

Create the file in `config / locales``` with the name ``` ja.yml```. (The file is read by matching the country name (ja in the case of Japan) following default_locale with 〇〇 in `〇〇.yml```) in config / locales)

3 Create a format in `` `ja.yml```


      default: '%-H:%M'

You can change the time notation to your liking by changing the description after default:.

4 Display the time set in 3 in the view file This is where the l method comes in. By using the l method, the time is displayed using the format specified in the yml file. To use it, add l to the head.


<%= l @laundry.open_time %> 〜 <%= l @laundry.close_time %>

Now you can display your preference in time notation without writing `` `strftime``` directly in the view file.

view (strftime direct writing)

 <%= @laundry.open_time.strftime('%-H:%M')%> 〜 <%= @laundry.close_time.strftime('%-H:%M')%> 

view (using l method)

<%= l @laundry.open_time %> 〜 <%= l @laundry.close_time %>

Use the l method to change the look to be neat! !! By the way, it is also possible to prepare multiple formats and use them properly (see the reference article below).

Reference article


Recommended Posts

Use the l method for time notation
Use Java external library for the time being
[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
How to use the link_to method
How to use the include? method
How to use the form_with method
Spring Boot for the first time
Spring AOP for the first time
Introduction to java for the first time # 2
[Rails] How to use the map method
[Java] How to use the toString () method
Learning for the first time java [Introduction]
I tried using Docker for the first time
Walls hit by Rspec for the first time
When you want to use the method outside
About the method
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
I want you to use Scala as Better Java for the time being
How to use the replace () method (Java Silver)
Run Dataflow, Java, streaming for the time being
[Ruby basics] How to use the slice method
What should I use for the testing framework [Rails]
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
Try running Spring Cloud Config for the time being
Learning memo when learning Java for the first time (personal learning memo)
Command to try using Docker for the time being
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Output about the method # 2
abbreviation for ruby method
About the length method
About the authenticate method.
About the map method
About the ancestors method
About the to_s method.
How to use the getter / setter method (in object orientation)
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
Hello World with Ruby extension library for the time being
I want to use the sanitize method other than View.
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
With the software I've been making for a long time ...
Modeling a Digimon with DDD for the first time Part 1