Ruby 如何使用DateTime.now.strftime(“Y-%m-%d”)将日期时间设置为1个月前?

Ruby 如何使用DateTime.now.strftime(“Y-%m-%d”)将日期时间设置为1个月前?,ruby,datetime,Ruby,Datetime,我试图使用设置谷歌分析开始日期和结束日期来捕获过去30天内的独特访客。我已将end\u date=DateTime.now.strftime(“%Y-%m-%d”),如何将start\u date设置为30天前。根据锡人的建议更新: 在Ruby中: require 'date' end_date = DateTime.now start_date = end_date - 30 formatted_end_date = end_date.strftime("%Y-%m-%d") formatte

我试图使用设置谷歌分析开始日期和结束日期来捕获过去30天内的独特访客。我已将
end\u date=DateTime.now.strftime(“%Y-%m-%d”)
,如何将
start\u date
设置为30天前。

根据锡人的建议更新:

在Ruby中:

require 'date'
end_date = DateTime.now
start_date = end_date - 30
formatted_end_date = end_date.strftime("%Y-%m-%d")
formatted_start_date = start_date.strftime("%Y-%m-%d")
在Rails中或如果需要
活动\u支持/时间

使用days和ago方法:

start_date = 30.days.ago
代码如下:

end_date = DateTime.now
start_date = end_date - 30.days
formatted_end_date = end_date.strftime("%Y-%m-%d")
formatted_start_date = start_date.strftime("%Y-%m-%d")
这样,开始日期正好比结束日期早30天。如果您实例化两个DateTimes,它们之间的间隔不会正好是30天

可以在Rails环境之外通过以下行访问
days
ago
方法:

require 'active_support/time'

根据锡人的建议更新:

在Ruby中:

require 'date'
end_date = DateTime.now
start_date = end_date - 30
formatted_end_date = end_date.strftime("%Y-%m-%d")
formatted_start_date = start_date.strftime("%Y-%m-%d")
在Rails中或如果需要
活动\u支持/时间

使用days和ago方法:

start_date = 30.days.ago
代码如下:

end_date = DateTime.now
start_date = end_date - 30.days
formatted_end_date = end_date.strftime("%Y-%m-%d")
formatted_start_date = start_date.strftime("%Y-%m-%d")
这样,开始日期正好比结束日期早30天。如果您实例化两个DateTimes,它们之间的间隔不会正好是30天

可以在Rails环境之外通过以下行访问
days
ago
方法:

require 'active_support/time'

问题是您在创建
end\u date
时走得太远了

你把它变成一个字符串,它没有数学功能。相反,将其保留为DateTime,您将继承进行日期计算的功能:

require 'date'

datetime_now = DateTime.now
end_date = datetime_now.strftime("%Y-%m-%d") # => "2013-08-06"
end_date.class # => String

end_date = datetime_now # => #<DateTime: 2013-08-06T14:55:20-07:00 ((2456511j,78920s,731393000n),-25200s,2299161j)>
end_date - 30 # => #<DateTime: 2013-07-07T14:55:20-07:00 ((2456481j,78920s,731393000n),-25200s,2299161j)>
需要“日期”
datetime\u now=datetime.now
end_date=datetime_now.strftime(“%Y-%m-%d”)#=>“2013-08-06”
结束_date.class#=>字符串
结束日期=日期时间\u现在#=>#
结束日期-30#=>#

请注意,
end_date-30
返回的日期时间正好早于30天;时间部分被保留。一旦得到所需的值,就将该值转换为字符串。

问题是您在创建
结束日期时走得太远了

你把它变成一个字符串,它没有数学功能。相反,将其保留为DateTime,您将继承进行日期计算的功能:

require 'date'

datetime_now = DateTime.now
end_date = datetime_now.strftime("%Y-%m-%d") # => "2013-08-06"
end_date.class # => String

end_date = datetime_now # => #<DateTime: 2013-08-06T14:55:20-07:00 ((2456511j,78920s,731393000n),-25200s,2299161j)>
end_date - 30 # => #<DateTime: 2013-07-07T14:55:20-07:00 ((2456481j,78920s,731393000n),-25200s,2299161j)>
需要“日期”
datetime\u now=datetime.now
end_date=datetime_now.strftime(“%Y-%m-%d”)#=>“2013-08-06”
结束_date.class#=>字符串
结束日期=日期时间\u现在#=>#
结束日期-30#=>#

请注意,
end_date-30
返回的日期时间正好早于30天;时间部分被保留。获得所需的值后,将该值转换为字符串。

使用basic Ruby,您可以执行以下操作:

> irb
require 'date' # needed
today_minus_30  = Date.today.to_date - 30

# if you need the date as a string
date_as_string = today_minutes_30.strftime("%Y-%m-%d")

使用基本Ruby,您可以做到以下几点:

> irb
require 'date' # needed
today_minus_30  = Date.today.to_date - 30

# if you need the date as a string
date_as_string = today_minutes_30.strftime("%Y-%m-%d")

我意识到这个问题已经很老了,但这里有一个干净的方法可以帮助任何需要了解的人:

start_date = (Date.now - 30.days).strftime("%Y-%m-%d")


您可以使用
DateTime.now
Time.now

执行类似的操作。我意识到这个问题已经很老了,但对于需要了解的任何人,这里有一个干净的方法:

start_date = (Date.now - 30.days).strftime("%Y-%m-%d")


您可以使用
DateTime.now
Time.now

执行类似的操作。我建议您使用助手方法。差不多

# Gets time range for x number timeunits ago
  def time_range(unit, timeunit = nil)
    if timeunit == "weeks"
      now = Time.zone.now.beginning_of_week
    elsif timeunit == "months"
      now = Time.zone.now.beginning_of_month
    else
      now = Time.zone.now.beginning_of_day
    end
    # Ex: time_range(0, "days") --> Get the time range for today  between the beginning of today and the beginning of tommorow - 1 second
    now - unit.send(timeunit)..now + 1.send(timeunit) - 1.seconds - unit.send(timeunit)
  end
将帮助您请求时间范围。所以当你要求像这样的东西时

time_range(0, "months")
返回0个月前(本月)的时间范围

然后您可以简单地查询数据库对象,并使用

Visit.where(started_at: time_range(unit, timeunit)).count

我建议使用助手方法来实现这一点。差不多

# Gets time range for x number timeunits ago
  def time_range(unit, timeunit = nil)
    if timeunit == "weeks"
      now = Time.zone.now.beginning_of_week
    elsif timeunit == "months"
      now = Time.zone.now.beginning_of_month
    else
      now = Time.zone.now.beginning_of_day
    end
    # Ex: time_range(0, "days") --> Get the time range for today  between the beginning of today and the beginning of tommorow - 1 second
    now - unit.send(timeunit)..now + 1.send(timeunit) - 1.seconds - unit.send(timeunit)
  end
将帮助您请求时间范围。所以当你要求像这样的东西时

time_range(0, "months")
返回0个月前(本月)的时间范围

然后您可以简单地查询数据库对象,并使用

Visit.where(started_at: time_range(unit, timeunit)).count

我们需要看到示例代码,显示您尝试过的内容。看,问题不是所有的蛾类都是30天。我想知道有什么解决方案吗?我们需要看到示例代码来展示您的尝试。看,问题不是所有的蛾类都是30天。我想知道有什么解决办法吗?您可能想解释
的来源以及如何将它们包括在内。OP使用的是纯Ruby,因此这些选项不可用。@theTinMan-我同意你的评论,并相应地更新了我的答案。谢谢。不,不,不,不,不要建议加载整个活动支持代码库,只是为了挑选一些需要的方法。将核心扩展用于、和。事实上,请阅读“.”theTinMan-再次更新,谢谢您的评论。我无法让
要求'active\u support/core\u ext/date.rb'
正常工作,但在此线程上找到了
要求'active\u support/time'
解决方法:谢谢!您可能需要解释
的来源以及如何包含它们。OP使用的是纯Ruby,因此这些选项不可用。@theTinMan-我同意你的评论,并相应地更新了我的答案。谢谢。不,不,不,不,不要建议加载整个活动支持代码库,只是为了挑选一些需要的方法。将核心扩展用于、和。事实上,请阅读“.”theTinMan-再次更新,谢谢您的评论。我无法让
要求'active\u support/core\u ext/date.rb'
正常工作,但在此线程上找到了
要求'active\u support/time'
解决方法:谢谢!我认为这需要
active\u支持/time
或rails?我认为这需要
active\u支持/time
或rails?