如何反向循环日期范围?

发布于 2024-09-01 10:29:28 字数 433 浏览 5 评论 0原文

我有一个日期范围,我希望能够反向循环。给出以下内容,我将如何完成此操作,标准 Range 运算符似乎无法正常工作。

>> sd = Date.parse('2010-03-01')
=> Mon, 01 Mar 2010
>> ed = Date.parse('2010-03-05')
=> Fri, 05 Mar 2010
>> (sd..ed).to_a
=> [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar     2010]
>> (ed..sd).to_a
=> []

正如您所看到的,范围运算符从开始到结束可以正常工作,但从结束到开始却不能正常工作。

I have a date range that I would like to be able to loop through in reverse. Give the following, how would I accomplish this, the standard Range operator doesn't seem t be working properly.

>> sd = Date.parse('2010-03-01')
=> Mon, 01 Mar 2010
>> ed = Date.parse('2010-03-05')
=> Fri, 05 Mar 2010
>> (sd..ed).to_a
=> [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar     2010]
>> (ed..sd).to_a
=> []

as you can see, the range operator works properly form start to end, but not from end to start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-09-08 10:29:28

尝试 upto/downto :

irb(main):003:0> sd = Date.parse('2010-03-01')
=> #<Date: 4910513/2,0,2299161>
irb(main):004:0> ed = Date.parse('2010-03-15')
=> #<Date: 4910541/2,0,2299161>
irb(main):005:0> sd.upto(ed) { |date| puts date }
2010-03-01
2010-03-02
2010-03-03
2010-03-04
2010-03-05
2010-03-06
2010-03-07
2010-03-08
2010-03-09
2010-03-10
2010-03-11
2010-03-12
2010-03-13
2010-03-14
2010-03-15
=> #<Date: 4910513/2,0,2299161>
irb(main):006:0> ed.downto(sd) { |date| puts date }
2010-03-15
2010-03-14
2010-03-13
2010-03-12
2010-03-11
2010-03-10
2010-03-09
2010-03-08
2010-03-07
2010-03-06
2010-03-05
2010-03-04
2010-03-03
2010-03-02
2010-03-01
=> #<Date: 4910541/2,0,2299161>

Try upto/downto :

irb(main):003:0> sd = Date.parse('2010-03-01')
=> #<Date: 4910513/2,0,2299161>
irb(main):004:0> ed = Date.parse('2010-03-15')
=> #<Date: 4910541/2,0,2299161>
irb(main):005:0> sd.upto(ed) { |date| puts date }
2010-03-01
2010-03-02
2010-03-03
2010-03-04
2010-03-05
2010-03-06
2010-03-07
2010-03-08
2010-03-09
2010-03-10
2010-03-11
2010-03-12
2010-03-13
2010-03-14
2010-03-15
=> #<Date: 4910513/2,0,2299161>
irb(main):006:0> ed.downto(sd) { |date| puts date }
2010-03-15
2010-03-14
2010-03-13
2010-03-12
2010-03-11
2010-03-10
2010-03-09
2010-03-08
2010-03-07
2010-03-06
2010-03-05
2010-03-04
2010-03-03
2010-03-02
2010-03-01
=> #<Date: 4910541/2,0,2299161>
紫轩蝶泪 2024-09-08 10:29:28

我通常只是反转结果数组:

ruby-1.8.7-p72 > sd = Date.parse('2010-03-01')
 => Mon, 01 Mar 2010 
ruby-1.8.7-p72 > ed = Date.parse('2010-03-05')
 => Fri, 05 Mar 2010 
ruby-1.8.7-p72 > (sd..ed).to_a
 => [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar 2010] 
ruby-1.8.7-p72 > (sd..ed).to_a.reverse
 => [Fri, 05 Mar 2010, Thu, 04 Mar 2010, Wed, 03 Mar 2010, Tue, 02 Mar 2010, Mon, 01 Mar 2010] 

我想,当您不知道开始日期是在结束日期之前还是之后时,为了让它做正确的事情,您需要以下内容

def date_range(sd, ed)
  sd < ed ? (sd..ed).to_a : (ed..sd).to_a.reverse
end

:无论哪种方式都会给你正确的东西:

ruby-1.8.7-p72 > sd = Date.parse('2010-03-01')
 => Mon, 01 Mar 2010 
ruby-1.8.7-p72 > ed = Date.parse('2010-03-05')
 => Fri, 05 Mar 2010 
ruby-1.8.7-p72 > date_range(sd, ed)
 => [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar 2010] 
ruby-1.8.7-p72 > date_range(ed, sd)
 => [Fri, 05 Mar 2010, Thu, 04 Mar 2010, Wed, 03 Mar 2010, Tue, 02 Mar 2010, Mon, 01 Mar 2010] 

I usually just reverse the resulting array:

ruby-1.8.7-p72 > sd = Date.parse('2010-03-01')
 => Mon, 01 Mar 2010 
ruby-1.8.7-p72 > ed = Date.parse('2010-03-05')
 => Fri, 05 Mar 2010 
ruby-1.8.7-p72 > (sd..ed).to_a
 => [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar 2010] 
ruby-1.8.7-p72 > (sd..ed).to_a.reverse
 => [Fri, 05 Mar 2010, Thu, 04 Mar 2010, Wed, 03 Mar 2010, Tue, 02 Mar 2010, Mon, 01 Mar 2010] 

I guess, to make it do the right thing when you don't know if the start date is going to be before or after the end date, you'd want something along the lines of:

def date_range(sd, ed)
  sd < ed ? (sd..ed).to_a : (ed..sd).to_a.reverse
end

which will give you the right thing either way:

ruby-1.8.7-p72 > sd = Date.parse('2010-03-01')
 => Mon, 01 Mar 2010 
ruby-1.8.7-p72 > ed = Date.parse('2010-03-05')
 => Fri, 05 Mar 2010 
ruby-1.8.7-p72 > date_range(sd, ed)
 => [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar 2010] 
ruby-1.8.7-p72 > date_range(ed, sd)
 => [Fri, 05 Mar 2010, Thu, 04 Mar 2010, Wed, 03 Mar 2010, Tue, 02 Mar 2010, Mon, 01 Mar 2010] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文