Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
11
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@Yinaura

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

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) オプションはないので注意。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
11
Help us understand the problem. What are the problem?