我需要在变量中获取三个日期
今天从午夜开始
t = Time.now.strftime("%Y-%m-%d 00:00:01") # 2012-11-19 00:00:01
昨天午夜后(即 00:00:00 到 23:59:59)
y1 = 2012-11-18 00:00:01
y2 = 2012-11-19 23:59:59
它特别是我需要创建为字符串以在 gem 中使用的 y1 和 y2 变量。作为 ruby 的新手,我有点困惑,因为 Time.yesterday 似乎没有做我需要的事情
编辑
为此一定要包括
require 'active_support/all'
并确保 gem 已为您的应用程序捆绑。
用过的:
@current = (Time.now).beginning_of_day.utc.strftime("%Y-%m-%d")
@yesterday = (Time.now - 1.day).beginning_of_day.utc.strftime("%Y-%m-%d")
@everything = (Time.now - 2.day).end_of_day.utc.strftime("%Y-%m-%d")
最佳答案
你可以做到,
t=Time.now
y1=t-1.day
y2=t+1.day
关于ruby-on-rails - Ruby Time.now 和其他日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453473/