我用案例陈述检查时间。怎么做?
问问题
1211 次
2 回答
3
使用范围:
case time
when (Time.now - 60)..(Time.now) then puts 'within the last minute'
when (Time.now - 3600)..(Time.now) then puts 'within the last hour'
end
范围适用于各种值。你也可以使用Date
s :
case date
when (Date.today - 1)..(Date.today) then puts 'less than a day ago'
when (Date.today - 30)..(Date.today) then puts 'less than a month ago'
end
更新:Ruby 1.9 打破了时间范围,因此该示例仅适用于 Ruby 1.8.7。不过,日期示例在两个版本中都适用。在 1.9 中,您可以使用此代码来匹配时间:
case time.to_i
when ((Time.now - 60).to_i)..(Time.now.to_i) then puts 'within the last minute'
when ((Time.now - 3600).to_i)..(Time.now.to_i) then puts 'within the last hour'
end
于 2011-01-14T06:05:03.220 回答
0
只需使用顶部没有定义变量的版本...
t = event.time # arbitrary example.
case
when t <= Time.now
# Event is in the past.
else
# Event is in the future.
end
于 2011-01-14T05:57:42.210 回答