Format the date and time given by created_at

I was worried about formatting created_at generated by sql, so a memo of how to implement it

sql file

Create a table.

table.sql


create table todos
(
  id integer PRIMARY key,
  body text,
  created_at
);

rb file

Use active record to bind the todos table created earlier to the object.

tlist.rb


class Todos < ActiveRecord::Base
  validates :body, presence: true
end

Store the object in an instance variable.

tlist.rb


get '/' do
  @todos = Todos.all
  erb :index
end

erb file

Try to display the time.

index.erb


  <div class="user_box">
    <p class="user_name">Shuto</p>
    <ul>
      <% @todos.where(users_userName: "Shuto").each do |todo|%>
      <li data-id = "<%= todo.id%>" data-token = "<%= Rack::Csrf.csrf_token(env)%>">
          <div class="one">
            <%= Rack::Utils.escape_html(todo.body)%>
          </div>
          <div class="time">
            <%= todo.created_at%>
            <span class="delete">[x]</span>
          </div>
      </li>
      <% end%>
    </ul>
  </div>

When I executed it, there are unnecessary parts, so I want to format it. スクリーンショット 2020-07-02 22.21.28.png

After investigating, it seems that it can be formatted with the strftime method. Apply and execute the method.

index.erb


  <div class="user_box">
    <p class="user_name">Shuto</p>
    <ul>
      <% @todos.where(users_userName: "Shuto").each do |todo|%>
      <li data-id = "<%= todo.id%>" data-token = "<%= Rack::Csrf.csrf_token(env)%>">
          <div class="one">
            <%= Rack::Utils.escape_html(todo.body)%>
          </div>
          <div class="time">
            <%= todo.created_at.strftime("%Y/%m/%d %H:%M:%S")%>
            <span class="delete">[x]</span>
          </div>
      </li>
      <% end%>
    </ul>
  </div>

I was angry that it was a character string.

スクリーンショット 2020-07-02 22.22.14.png

It seems that it is necessary to convert it to something other than a character string, so try converting it to date type.

index.erb


  <div class="user_box">
    <p class="user_name">Shuto</p>
    <ul>
      <% @todos.where(users_userName: "Shuto").each do |todo|%>
      <li data-id = "<%= todo.id%>" data-token = "<%= Rack::Csrf.csrf_token(env)%>">
          <div class="one">
            <%= Rack::Utils.escape_html(todo.body)%>
          </div>
          <div class="time">
            <%= todo.created_at.to_date.strftime("%Y/%m/%d %H:%M:%S")%>
            <span class="delete">[x]</span>
          </div>
      </li>
      <% end%>
    </ul>
  </div>

The date is correct, but the time is incorrect. スクリーンショット 2020-07-02 22.23.14.png

Convert it to time type and execute it.

index.erb


  <div class="user_box">
    <p class="user_name">Shuto</p>
    <ul>
      <% @todos.where(users_userName: "Shuto").each do |todo|%>
      <li data-id = "<%= todo.id%>" data-token = "<%= Rack::Csrf.csrf_token(env)%>">
          <div class="one">
            <%= Rack::Utils.escape_html(todo.body)%>
          </div>
          <div class="time">
            <%= todo.created_at.to_time.strftime("%Y/%m/%d %H:%M:%S")%>
            <span class="delete">[x]</span>
          </div>
      </li>
      <% end%>
    </ul>
  </div>

It was displayed properly. スクリーンショット 2020-07-02 22.23.43.png

You need to find out what format it is given when you face an error.

The prototype of this application is based on Introduction to sinatra of dot installation.

Recommended Posts

Format the date and time given by created_at
Date and time
Get the current date and time by specifying the time zone in Thymeleaf
Parse the date and time string formatted by the C asctime function in Java
What is the LocalDateTime class? [Java beginner] -Date and time class-
Set the date and time from the character string with POI
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[MySQL] [java] Receive date and time
Check the actual date and time at parse with Java's SimpleDateFormat
The design concept of Java's Date and Time API is interesting
The relationship between strict Java date checking and daylight savings time
Add the date to the GC statistics acquired by gcutil and output it.
Explanation of Ruby Time and Date objects
[Java] How to set the Date time to 00:00:00
How to display today's date and time by default: Remove seconds or less
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Walls hit by Rspec for the first time
Date and time acquisition method using DateAndTime API
The date time of java8 has been updated
Format of the log output by Tomcat itself in Tomcat 8
Speed comparison at the time of generation at the time of date conversion
Change date and time to Japanese notation in Rails
[Rails] Precautions when comparing date and time with DateTime
Learn for the first time java # 3 expressions and operators
Usability of date and time classes in each language
[Kotlin] Convert ZonedDateTime to String by specifying the format
Handling of date and time in Ruby. Use Date and Time properly.
[Rails] Validate the start time (datetime type) and end time