[Ruby] Format the date and time given in created_at

1 minute read

Since I was worried about formatting the created_at generated by sql, a note on how to implement it

sql file

Create a table.

table.sql


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

rb file

Using the active record, connect 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

Display the time.

index.erb


  <div class="user_box">
    <p class="user_name">Syuto</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 execute it, there are unnecessary parts, so I want to format it. ![Screenshot 2020-07-02 22.21.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/644402/b56a1d44-9314-f5b7-022c-(c7f4106fd43b.png)

When I looked it up, it seems that it can be formatted with the strftime method. Apply the method and execute.

index.erb


  <div class="user_box">
    <p class="user_name">Syuto</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 string.

![Screenshot 2020-07-02 22.22.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/644402/8227de98-b1e7-b329-ef10-(11e34eb92145.png)

It seems that it needs to be converted to something other than a character string, so try converting it to date type.

index.erb


  <div class="user_box">
    <p class="user_name">Syuto</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 wrong. ![Screenshot 2020-07-02 22.23.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/644402/1f88e837-772a-617b-317c-(43993a0986dd.png)

Convert to time type and try again.

index.erb


  <div class="user_box">
    <p class="user_name">Syuto</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. ![Screenshot 2020-07-02 22.23.43.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/644402/a4390d33-6f3f-a35a-0590-(0489825c516d.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.