如何知道今天的日期是在日期范围内? [英] How to know if today's date is in a date range?

查看:118
本文介绍了如何知道今天的日期是在日期范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件与 start_time end_time ,并要检查事件是否正在进行。这将是检查今天的日期是否在两个日期之间的范围。

I have an event with start_time and end_time and want to check if the event is "in progress". That would be to check if today's date is in the range between the two dates.

您将如何在函数中执行此操作?

How would you do this in a function?

推荐答案

使用 ===






实际上,有一个操作员会这样做。使用 === 并将时间 $ c> operator。

Use ===


Actually, there is an operator that will do this. Make a Range and compare Time objects to it using the === operator.

start   = Time.now.to_i

range   = start..(start + 2)
inside  = start + 1
outside = start + 3        # ok, now...

range === inside  # true
range === outside # false



comment-flood:这个版本在各地都很好用。 (在Rails中,在Ruby 1和Ruby 2中)。较早的 irb 示例也可以正常工作,但是在一些实验中,交互式示例并不总是正确复制。


Update post-comment-flood: This version works well everywhere. (In Rails, in Ruby 1, and in Ruby 2.) The earlier irb example also worked fine but the interactive example wasn't always reproduced correctly in some experiments. This one is easier to cut-and-paste.

现在已经整理了。

It's all straightened out now.

这篇关于如何知道今天的日期是在日期范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆