[RUBY] I want to get only the time from Time type data ...! [Strftime] * Additional notes

Introduction / Situation when a problem occurs

Development environment

Ruby 2.6.5 Rails 6.0.3.4 MySQL Visual Studio Code (GoogleChrome)

I'm creating something like a calendar app. I want to save the date and start / end time separately in the DB, so I created a form using time_select in the time type column for the time.

new.html.erb(Excerpt only for time form)


<div class="start-date-input">
  <div class="form-title">
Start time
    <span class="indispensable">Mandatory</span>
  </div>
  <div class='input-date-wrap'>
    <%= f.time_select(:start_time, {class:'select-date', id:"start-time", prompt:'--', time_separator: ':'}, ignore_data: true) %>
  </div>
</div>
<div class="start-date-input">
  <div class="form-title">
ending time
    <span class="indispensable">Mandatory</span>
  </div>
  <div class='input-date-wrap'>
    <%= f.time_select(:start_time, {class:'select-date', id:"start-time", prompt:'--', time_separator: ':'}, ignore_data: true) %>
  </div>
</div>
form

If you enter the date and time in the form as above and submit the data as a trial,

db

It seems that the date and time and each time are safely saved in the DB. Then when I try to display it in the view ...

show.html.erb


<div class="event-show-info-left">
  <p><%= @event.day %></p>
  <p><strong><%= @event.start_time %></strong></p>
  <p><strong>↓</strong></p>
  <p><strong><%= @event.finish_time %></strong></p>
</div>
before

!!!?!!!?

The mysterious year and month are also displayed for each time ... Who the hell is this 20th century child ...

Consideration and verification of the cause

Once, I checked the params of the transmitted data.

[1] pry(#<EventsController>)> params
=> <ActionController::Parameters {
(abridgement)

 "day(1i)"=>"2020", "day(2i)"=>"12", "day(3i)"=>"2", 
"start_time(1i)"=>"1", "start_time(2i)"=>"1", "start_time(3i)"=>"1", "start_time(4i)"=>"10", "start_time(5i)"=>"00", 
"finish_time(1i)"=>"1", "finish_time(2i)"=>"1", "finish_time(3i)"=>"1", "finish_time(4i)"=>"13", "finish_time(5i)"=>"30", 

(abridgement)
} permitted: false>

(1i)… year (2i)… Moon (3i)… Sun (4i)… time (5i)… minutes As you can see, the date is given along with each time.

Since it saves time, it is set to time type, so it will not be reflected in the DB, As a specification of time_select, it can be considered that it is handled with" 1 "added to the date. This "1" is fetched into the view as it is, which seems to be the cause of the example 2000-01-01.

** Then, how do you get only the time ...? ~ **

I would like to devise something on the view side. I checked ...

[Official] Ruby 2.7.0 Reference Manual instance method Time # strftime

strftime(format) -> String Returns the result of converting the time to a string according to the format string.

With reference to the above, strftime seems to play an active role! Use strftime for the start time / end time class and format it into a string.

result

Since we only get the time, we use the following format.

--% H: 24-hour clock (00-23) --% M: Minutes (00-59)

I added .strftime ("% H:% M ") to the view file.

show.html.erb(Fix)


<div class="event-show-info-left">
  <p><%= @event.day %></p>
  <p><strong><%= @event.start_time.strftime( "%H:%M" ) %></strong></p>
  <p><strong>↓</strong></p>
  <p><strong><%= @event.finish_time.strftime( "%H:%M" ) %></strong></p>
</div>
after

Only the time could be displayed !!

At the end / impression

strftime and time_select were a great combination!

Regarding the handling of time, when it comes to large-scale implementation in the future I also felt that special consideration such as time zone and leap year would be required. I would like to take this opportunity to deepen my understanding of Time class classes.

This is a poor article for beginners, but I hope it will be of some help to you. Thank you for reading to the end.

Postscript

11/11 21:40 Regarding the date and time format, from the aspect of DRY, I was instructed that it is better to correspond with the time zone and locale setting and the l method. Please refer to the article below.

[For beginners, with video] Use the l method rather than strftime when formatting the date and time in Rails

Thank you @jnchito!

Reference article

[Official] Ruby 2.7.0 Reference Manual instance method Time # strftime

[[Official] Ruby on Rails 6.0.3.4 Module ActionView :: Helpers :: DateHelper](https://api.rubyonrails.org/v6.0.3.4/classes/ActionView/Helpers/DateHelper.html#method-i- date_select)

[Qiita] What is strftime?

[Qiita] Display the value of Time type data only in the time part

[Hatena Blog] Have time_select enter only time [rails]

Recommended Posts

I want to get only the time from Time type data ...! [Strftime] * Additional notes
[Android] I want to get the listener from the button in ListView
I want to get the value in Ruby
[Java] I want to calculate the difference from the date
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I want to distinct the duplicated data with has_many through
Change only one character from the String type string to uppercase
[IOS14] How to get Data type image data directly from PHPickerViewController?
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to give edit and delete permissions only to the poster
[Java] How to convert from String to Path type and get the path
[Ruby] I want to output only the odd-numbered characters in the character string
Glassfish tuning list that I want to keep for the time being
[IOS] How to get data from DynamoDB
[Ruby] I want to extract only the value of the hash and only the key
How to compare only the time with Rails (from what time to what time, something like)
I want to get the field name of the [Java] field. (Old tale tone)
I translated the grammar of R and Java [Updated from time to time]
I want to find out what character the character string appears from the left
I want to get the value of Cell transparently regardless of CellType (Apache POI)
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
Development memo ~ I want to display only the first image posted multiple times ~
I tried to get the distance from the address string to the nearest station with ruby
I want you to use Scala as Better Java for the time being
How to get the date from the JavaScript Date type that C # developers are addicted to
I want to recursively get the superclass and interface of a certain class
I want to output the day of the week
Run R from Java I want to run rJava
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to var_dump the contents of the intent
I want to play with Firestore from Rails
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to output the character number from the left where an arbitrary character string appears.
[Swift] Use Swity JSON to easily get information from JSON data of the fortune-telling API
I want to see only the latest because the Docker log has become too large
Enum misconfiguration. For some reason, I can't get data from the column specified by Enum ...
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
[Ruby] Method to easily get the receiver type .class
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to call the main method using reflection
I want to add a reference type column later
[Rough commentary] I want to marry the pluck method
I want to get along with Map [Java beginner]
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to redirect sound from Ubuntu with xrdp
I want to simplify the log output on Android
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
I want to fetch another association of the parent model from the intermediate table with has_many
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
I want to get a list of the contents of a zip file and its uncompressed size
I want to set the video playback time (HH: MM: SS) to seconds, and vice versa.