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

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

问题描述

我有一个带有 start_timeend_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?

推荐答案

使用===

<小时>

实际上,有一个运营商会这样做.创建一个 Range 并使用 === 运算符将 Time 对象与其进行比较.

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

<小时>更新 post-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天全站免登陆