找出当前时间是否在两次之间 [英] Find out if current time is between two times

查看:91
本文介绍了找出当前时间是否在两次之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Postgresql数据库中存储了两个时间列: open_time close_time 。我试图找出当前时间(忽略日期)是否介于两次之间(忽略日期)。

I have two time columns stored in a Postgresql database: open_time and close_time. I'm trying to find out if the current time, ignoring the date, is between the two times, ignoring the dates.

此代码比较日期以及时间:

This code compares the dates as well as the time:

current_time = Time.now
if current_time.between?(store.open_time, store.close_time)
  puts "IN BETWEEN"      
end

例如,它不起作用,当 current_time#=>时, 2018-06-06 23:59:49 -0600 open_time#=> 2000-01-01 22:59:00 UTC

It doesn't work, for example, when current_time # => 2018-06-06 23:59:49 -0600 and open_time # => 2000-01-01 22:59:00 UTC.

如何获取不包含日期的信息,只比较时间?

How do I get it to not include the dates, and just compare the times?

推荐答案

也许您想要这样的东西:

Perhaps you want something like this:

current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time -= current_time.beginning_of_day
open_time -= open_time.beginning_of_day
close_time -= close_time.beginning_of_day
if current_time.between?(open_time, close_time)
  puts "IN BETWEEN"      
end

current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time = [current_time.hour, current_time.min, current_time.sec]
open_time = [open_time.hour, open_time.min, open_time.sec]
close_time = [close_time.hour, close_time.min, close_time.sec]
if open_time <=> current_time == -1 and current_time <=> close_time == -1
  puts "IN BETWEEN"      
end

这篇关于找出当前时间是否在两次之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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