[RUBY] [Rails] strftime this and that

Preface

I'm currently creating a matching app with Rails, but I use strftime a lot, so I'll summarize how to write it so that you can see it immediately.

What is strftime?

It is a method that can display the date and time data in the view as you like.

let's try it

This time we will do it with created_at.

This is the default. It seems that +0900 at the back is not used. 2020-05-22 16:11:01 +0900

created_at.strftime("%Y-%-m-%-d %-H:%M")

#Display 2020-5-28 20:25

You can convert the character string like this. The symbols are not arranged properly. Lol

I have attached a part of the form below. If you want to know more, please click here. Source pikawaka

Format Return value Description
%Y 2019 Obtain the year as a 4-digit number * "0001" for the first year of the year
%m 01 Be sure to get the month as a two-digit number (01)-12)
%-m 1 Get the month as a 1- or 2-digit number (1)-12)
%d 01 Be sure to get the date in 2 digits (01), 02 ... )
%-d 1 Get the date in 1 or 2 digits (1), 2 ... 10, 11 ...)
%H 12 Always get the 24-hour time in 2 digits (00)-23)
%-H 12 Get the 24-hour time in 1 or 2 digits (0)-23)
%I 12 Get the 12-hour time in 1 or 2 digits (1)-12)
%M 00 Get minutes (00)-59)
%S 00 Get seconds (00)-60) * 60 is a leap second

Thank you very much!

Recommended Posts

[Rails] strftime this and that
Digital certificate this and that
Base64 encoder this and that
This and that of Core Graphics
This and that about Base64 (Java)
This and that of exclusive control
This and that of Swift corner Radius
Rails and FormData
Rails valid? And invalid?
Rails CRUD function implementation ② (edited and detailed this time)
[Rails] Volume that displays favorites and a list of favorites
This and that for editing ini in Java. : inieditor-java
[Rails] N + 1 problems and countermeasures
Rails: Difference between resources and resources
Rails Posts and User Linkage
[Rails] require method and permit method
Rails "render method" and "redirect method"
rails path and url methods
I tried to verify this and that of Spring @ Transactional
Introducing Bootstrap and Font-Awesome (Rails)
Rails is difficult and painful! Ⅱ
Rails web server and application server
[Rails] Save start time and end time
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[Rails] Difference between find and find_by
[Rails] Validation settings and Japanese localization
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Rails] Differences and usage of each_with_index and each.with_index
Project ruby and rails version upgrade
Consideration about Rails and Clean Architecture
String operations this and that-Java edition-
[rails] Difference between redirect_to and render
This and that of the implementation of date judgment within the period in Java
Launching the production environment with docker + rails (ver5.2) and errors that occurred