🖥

Rails: Date / Time / DateTime differences. And how to get a string for

2019/04/16に公開

Date type (= day of week + date)

Example: Tue, 20 Oct 2015

 Date.today # => Tue, 20 Oct 201 

Time type (= date + time)

Example: 2015-10-20 19:22:41 +0900

 Time.now # => 2015-10-20 19:22:41 +0900 

DateTime type (= day of week + date + time)

Example: Tue, 20 Oct 2015 19:22:43 +0900

 DateTime.now # => Tue, 20 Oct 2015 19:22:43 +0900 

String type

For example.

  • 2015-10-20
  • 2015-10-20 19:22:43

こ う い う These are just strings, not types that represent time.

To get such a string, to_s to_s(:db) to_s / to_s(:db) on the Date / Time / DateTime result.

Get date

 Date.today.to_s 

=> "2015-10-20"

Get the date and time

 Time.now.to_s(:db) 

=> "2015-10-20 19:22:43"

 DateTime.now.to_s(:db) 

=> "2015-10-20 19:22:43"

  • Note that raw ruby does not have the to_s (: db) option.

Original by

Rails: Date / Time / DateTime 型の違い。そしてSQL用の文字列を得る方法。

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion