setoya-blog

システム開発技術、データ分析関連でお勉強したことや、山奥生活を綴る、テンション低めなブログです。

ActiveSupportのto_time、to_date、to_datetimeが便利

マッチング系のロジック(5分以内ならマッチング可能とか)で日付をまたいだときの処理が結構大変なんだけど、今日はRailsのActiveSupportのto_date、to_time、to_datetimeの存在を知ったのでメモ。すごく便利だと思う。

$ rails c

ruby-1.9.2-p290 :003 > now = DateTime.now
 => Fri, 20 Jul 2012 14:09:34 +0900 

ruby-1.9.2-p290 :004 > date = now.to_date
 => Fri, 20 Jul 2012 

ruby-1.9.2-p290 :005 > date
 => Fri, 20 Jul 2012 

ruby-1.9.2-p290 :006 > today = Date.today
 => Fri, 20 Jul 2012 

ruby-1.9.2-p290 :007 > today
 => Fri, 20 Jul 2012 

ruby-1.9.2-p290 :008 > today == date
 => true 

ruby-1.9.2-p290 :009 > today.to_time
 => 2012-07-20 00:00:00 +0900