[英]Ruby Time.now and Other dates
I need to get three dates in variables 我需要在变量中得到三个日期
Today Since Midnight which I have as 今天午夜以来,我有
t = Time.now.strftime("%Y-%m-%d 00:00:01") # 2012-11-19 00:00:01
t = Time.now.strftime(“%Y-%m-%d 00:00:01”)#2012-11-19 00:00:01
Yesterday Since Midnight (ie 00:00:00 to 23:59:59) 昨天午夜(即00:00:00至23:59:59)
y1 = 2012-11-18 00:00:01
y1 = 2012-11-18 00:00:01
y2 = 2012-11-19 23:59:59y2 = 2012-11-19 23:59:59
it is specifically the y1 & y2 variables I need to create as strings for use in a gem. 特别是我需要将其创建为字符串以在gem中使用的y1和y2变量。 being new to ruby I am a little confused as Time.yesterday doesn't seem to do what I need
对ruby不熟悉我有点困惑,因为Time.yesterday似乎没有做我需要的东西
EDIT 编辑
For this be sure to include 为此,一定要包括
require 'active_support/all'
需要'active_support / all'
and ensure the gem is bundled for your application. 并确保宝石捆绑为您的应用程序。
Used: 用过的:
@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")
You can do, 你可以做,
t=Time.now
y1=t-1.day
y2=t+1.day
t = Time.now
t - 1.day # => yesterday
t + 1.day # => tomorrow
convert t to date first, 首先将t转换为日期,
t = t.to_date
t - 1.day # => yesterday
t + 1.day # => tomorrow
Since you are running Rails there's help to get from ActiveSupport 由于您正在运行Rails,因此可以从ActiveSupport获取帮助
1.day.ago.midnight
Time.new.beginning_of_day
You should also consider not using 2012-11-19 00:00:01
or 2012-11-19 23:59:59
. 您还应该考虑不使用
2012-11-19 00:00:01
或2012-11-19 23:59:59
。 Instead you could use 00:00:00 and compare with >= or < depending on what you want to achieve. 相反,您可以使用00:00:00并与> =或<进行比较,具体取决于您想要实现的目标。 If you always round to seconds then
2012-11-19 00:00:00
to 2012-11-19 23:59:59
would work. 如果你总是圆到秒,那么
2012-11-19 00:00:00
到2012-11-19 23:59:59
就行了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.