Ruby-on-rails 我怎样才能得到昨天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6490024/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:28:14  来源:igfitidea点击:

How I can get yesterday's date?

ruby-on-railsruby

提问by Michael Gorsky

How can I get yesterday's date?

我怎样才能得到昨天的日期?

maybe:

也许:

@get_time_now    = Time.now.strftime('%m/%d/%Y') / 86400 

or

或者

@get_time_now    = Time.now.strftime('%m/%d/%Y') - 1.day

or

或者

@get_time_now    = Time.now. / 86400 

86400 = 1 day, right? (60 * 60 * 24)

86400 = 1 天,对吗?(60*60*24)

回答by Andrew Nesbitt

Rails

导轨

For a date object you could use:

对于日期对象,您可以使用:

Date.yesterday

Or a time object:

或时间对象:

1.day.ago

Ruby

红宝石

Or outside of rails:

或轨道外:

Date.today.prev_day

回答by Koen.

After trying 1.day.agoand variants on it:

在尝试1.day.ago和变体之后:

irb(main):005:0> 1.day.ago
NoMethodError: undefined method `day' for 1:Fixnum

if found that Date.today.prev_dayworks for me:

如果发现Date.today.prev_day对我有用:

irb(main):016:0> Date.today.prev_day
=> #<Date: 2013-04-09 ((2456392j,0s,0n),+0s,2299161j)>

回答by AlejandroVD

Time.now - (3600 * 24)   # or Time.now - 86400

or

或者

require 'date'

Date.today.prev_day

回答by shadowbq

Ruby 2.1.2 Native Time

Ruby 2.1.2 本地时间

Answer:

回答:

Time.at(Time.now.to_i - 86400)

Proof:

证明:

2.1.2 :016 > Time.now
 => 2014-07-01 13:36:24 -0400 
2.1.2 :017 > Time.now.to_i
 => 1404236192 
2.1.2 :018 > Time.now.to_i - 86400
 => 1404149804 
2.1.2 :019 > Time.at(Time.now.to_i - 86400)
 => 2014-06-30 13:36:53 -0400 

One Day of Seconds.

一日一秒。

86400 = 1 day (60 * 60 * 24)

86400 = 1 天 (60 * 60 * 24)

回答by Sampat Badhe

use DateTime.now - 1

使用 DateTime.now - 1

1.9.3p194 :040 > DateTime.now
 => Mon, 18 Nov 2013 17:58:45 +0530 
1.9.3p194 :041 > DateTime.now - 1
 => Sun, 17 Nov 2013 17:58:49 +0530 

or DateTime.yesterday

或 DateTime.yesterday

1.9.3p194 :042 > DateTime.yesterday
 => Sun, 17 Nov 2013 

or we can use railsadvancemethod for Timeand DateTime

或者我们可以使用railsadvance方法TimeDateTime

1.9.3p194 :043 > Time.now.advance(days: -1)
 => 2013-11-17 17:59:36 +0530
1.9.3p194 :044 > DateTime.now.advance(days: -1)
 => Sun, 17 Nov 2013 17:59:49 +0530

advancemethod also provides this options :years, :months, :weeks, :days, :hours, :minutes, :seconds

advance方法也提供了这个选项 :years, :months, :weeks, :days, :hours, :minutes, :seconds

DateTime advance method

日期时间提前方法

Time advance method

时间提前法

回答by John Naegle

Use Date.today - 1.days.

使用 Date.today - 1.days。

Date.yesterday depends on the current time and your offset from GMT

Date.yesterday 取决于当前时间和您与 GMT 的偏移量

1.9.3-p125 :100 > Date.today
 => Wed, 29 Feb 2012 
1.9.3-p125 :101 > Date.yesterday
 => Wed, 29 Feb 2012 
1.9.3-p125 :102 > Date.today - 1.days
 => Tue, 28 Feb 2012 

回答by d11wtq

You can just subtract 86400 from a Timeobject to get one day before. If you are using Rails, or have ActiveSupport included, you can replace 86400 with 1.days.

您只需从Time对象中减去 86400即可获得前一天。如果您使用 Rails 或包含 ActiveSupport,则可以将 86400 替换为1.days.

If you're using a Dateobject, and not a Timeobject, just subtract 1 from it.

如果您使用的是一个Date对象,而不是一个Time对象,只需从它减去 1。

To check if one date/time is before/after another, just compare the two objects like you would do for numbers:

要检查一个日期/时间是否在另一个日期/时间之前/之后,只需比较两个对象,就像您对数字所做的那样:

DateTime.parse("2009-05-17T22:38:42-07:00") < DateTime.parse("2009-05-16T22:38:42-07:00")
# => false