红宝石的时间三角洲

时间:2018-05-17 10:40:38

标签: ruby-on-rails ruby datetime split

我有两个日期时间值。

我试图得到给定时间将在给定时隙中分割的结果。

time_delta(from_date,to_date,time_lapse)

例如:

from_date = '2018-05-18 00:00:00'
to_date = '2018-05-18 23:59:59'
time_delta(from_date, to_date, 1.hour)

期待回应: 回复= [ '2018-05-18 12 AM', '2018-05-18 1 AM', '2018-05-18 2 AM', ... '2018-05-18 12 PM', '2018-05-18 1 PM', ... '2018-05-18 10 PM', '2018-05-18 11 PM']

不确定如何得到这样的回应。

1 个答案:

答案 0 :(得分:0)

def time_delta(from_date, to_date, step)
  current_date = from_date
  dates = []
  while current_date <= to_date
    dates << current_date
    current_date += step
  end
  dates
end