Mike
Mike

Reputation: 19697

Rails DateTime.now without Time

I need to use DateTime.now to grab the current date, and "strip off" the time.

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

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

Upvotes: 108

Views: 151252

Answers (7)

Igor Kapkov
Igor Kapkov

Reputation: 3899

You can use one of the following:

  • DateTime.current.midnight
  • DateTime.current.beginning_of_day
  • DateTime.current.to_date

Upvotes: 200

Vlad Hilko
Vlad Hilko

Reputation: 1244

You can use just:

Date.current

Upvotes: 11

LordKiz
LordKiz

Reputation: 643

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

Upvotes: 4

Ernesto
Ernesto

Reputation: 4017

What about Date.today.to_time?

Upvotes: 9

Jon M
Jon M

Reputation: 11705

If you're happy to require 'active_support/core_ext', then you can use

DateTime.now.midnight # => Sat, 19 Nov 2011 00:00:00 -0800

Upvotes: 5

VP.
VP.

Reputation: 5141

What you need is the function strftime:

Time.now.strftime("%Y-%d-%m %H:%M:%S %Z")

Upvotes: 28

Mike
Mike

Reputation: 19697

Figured it out. This works:

DateTime.now.in_time_zone.midnight

Upvotes: 1

Related Questions