Rails DateTime.now without time

I need to use DateTime.now to capture the current date and โ€œcancelโ€ the time.

For example, this shows what I do not want: DateTime.now => Sat, 19 Nov 2011 18:54:13 UTC +00:00

This shows what I want: DateTime.now.some_operation => 2011-11-06 00:00:00 UTC

+64
ruby datetime ruby-on-rails
Nov 19 '11 at 18:55
source share
7 answers

Try DateTime.now.to_date or DateTime.now.beginning_of_day

+126
Nov 19 '11 at 19:01
source share

You need the strftime function:

 Time.now.strftime("%Y-%d-%m %H:%M:%S %Z") 
+15
Nov 19 '11 at 18:59
source share

What about Date.today.to_time ?

+7
Jul 16 '12 at 14:20
source share

If you are satisfied with require 'active_support/core_ext' , you can use

 DateTime.now.midnight # => Sat, 19 Nov 2011 00:00:00 -0800 
+4
Nov 19 '11 at 19:00
source share

If you want today's date without time, just use Date.today

+2
Aug 27 '16 at 13:27
source share

You can use only:

 Date.current 
+2
Aug 14 '17 at 9:37 on
source share

Figured it out. It works:

 DateTime.now.in_time_zone.midnight 
0
Nov 19 '11 at 18:58
source share



All Articles