ruby-on-rails - Ruby Time.now 和其他日期

标签 ruby-on-rails ruby strftime

我需要在变量中获取三个日期

今天从午夜开始

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/

相关文章:

ruby-on-rails - Rails 3 Active Record 关系顺序 : Use hash instead of string

css - 动态替换 css 标题图片

ruby-on-rails - 命名范围的验证

ruby - 为什么 strftime 中的 Ruby 时区指示符格式不起作用?

python - 将日期 8-DEC-17 转换为 08/12/2017 在 python 中给出 07/12/2017

localtime_r 的 C++11 替代方案

javascript - 通过 AJAX 更改 link_to 参数

ruby-on-rails - 使用 Ruby on Rails 登录 Facebook

mysql - Rails 在 has_many :through relation 中创建数据

ruby-on-rails - Rails - 验证记录而不添加错误