11
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2015-10-20

Date型 ( = 曜日 + 日付 )

例: Tue, 20 Oct 2015

Date.today
# => Tue, 20 Oct 201

Time型 ( = 日付 + 時刻 )

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

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

DateTime型 ( = 曜日 + 日付 + 時刻 )

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

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

String 型

たとえば。

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

↑こういうのはただの文字列で、時間を表す型ではない

こういう文字列を得るには、Date / Time / DateTime の結果に対して to_s / to_s(:db) をすれば良い。

日付を得る

Date.today.to_s

=> "2015-10-20"

日時を得る

Time.now.to_s(:db)

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

DateTime.now.to_s(:db)

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

 

  • 素の ruby では to_s(:db) オプションはないので注意。

チャットメンバー募集

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

メンター受付

11
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?