gpt4 book ai didi

ruby-on-rails - 在 Rails 的某些时间之间获取随机时间对象

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:43 26 4
gpt4 key购买 nike

我想要一个方法:

def time_between(from, to)
***
end

time_between 10.am.of_today, 3.pm.of_today # => 1pm Time object
time_between 10.am.of_today, 3.pm.of_today # => 3pm Time object
time_between 10.am.of_today, 3.pm.of_today # => 10:30am Time object
# I mean random

这里有两个问题:如何实现***?以及如何实现x.pm.of_today

最佳答案

ruby 1.9.3

require 'rubygems'
require 'active_support/all'

def random_hour(from, to)
(Date.today + rand(from..to).hour + rand(0..60).minutes).to_datetime
end

puts random_hour(10, 15)

关于ruby-on-rails - 在 Rails 的某些时间之间获取随机时间对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com