Ruby-on-rails Rails DateTime.now 没有时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8196434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails DateTime.now without Time
提问by Mike
I need to use DateTime.now to grab the current date, and "strip off" the time.
我需要使用 DateTime.now 来获取当前日期,并“剥离”时间。
For example, this shows what I don'twant: DateTime.now => Sat, 19 Nov 2011 18:54:13 UTC +00:00
例如,这显示了我不想要的内容:DateTime.now => Sat, 19 Nov 2011 18:54:13 UTC +00:00
This shows what I dowant: DateTime.now.some_operation => 2011-11-06 00:00:00 UTC
这说明什么,我做想要的:DateTime.now.some_operation => 2011-11-06 00:00:00 UTC
回答by Igor Kapkov
You can use one of the following:
您可以使用以下方法之一:
DateTime.current.midnightDateTime.current.beginning_of_dayDateTime.current.to_date
DateTime.current.midnightDateTime.current.beginning_of_dayDateTime.current.to_date
回答by VP.
What you need is the function strftime:
你需要的是函数strftime:
Time.now.strftime("%Y-%d-%m %H:%M:%S %Z")
回答by Ernesto
What about Date.today.to_time?
怎么样Date.today.to_time?
回答by Jon M
If you're happy to require 'active_support/core_ext', then you can use
如果您愿意require 'active_support/core_ext',那么您可以使用
DateTime.now.midnight # => Sat, 19 Nov 2011 00:00:00 -0800
回答by LordKiz
If you want today's date without the time, just use Date.today
如果你想要没有时间的今天的日期,只需使用 Date.today
回答by Evan Ross
You can use just:
您可以只使用:
Date.current
回答by Mike
Figured it out. This works:
弄清楚了。这有效:
DateTime.now.in_time_zone.midnight

