如何以15分钟的间隔迭代开始和结束时间

时间:2013-05-25 19:52:58

标签: ruby-on-rails-3 datetime iteration

如何:迭代表行,每行标记一次。第一行是开始时间,最后一行是结束时间,迭代应该以15分钟的间隔创建它们之间的每一行。

start_time:'06:00',end_time:'07:00'

06:00

6时15分

06:30

06:45

07:00

更新

start_time = Time.local(2013, 5, 25, 06, 00) 
  end_time = Time.local(2013, 5, 25, 20, 00) 

  begin
    start_time += 15.minutes
    puts start_time
  end while start_time < end_time

这会返回nil ...但是,不应该......它应该返回值

2 个答案:

答案 0 :(得分:1)

我想出的就是我需要的东西。受到jethroos答案的启发。

def cal_times

start_time = Time.local(2013, 5, 25, 06, 00) 
  end_time = Time.local(2013, 5, 25, 20, 00) 
     times = [start_time.strftime('%H:%M')]

  begin
    start_time += 15.minutes
    times << start_time.strftime('%H:%M')
  end while start_time < end_time

  times

end

答案 1 :(得分:0)

我不确定我是否理解正确的问题,但这里有一个例子,说明如何在15分钟的间隔内生成时间

1.9.3p392 :029 > a = Time.now
=> 2013-05-25 23:39:44 +0200 
1.9.3p392 :030 > a = a - a.sec - a.min%15*60
=> 2013-05-25 23:30:00 +0200 
1.9.3p392 :031 > b = a + 1.hour
=> 2013-05-26 00:30:00 +0200 
1.9.3p392 :032 > begin
1.9.3p392 :033 >     a += 15.minutes
1.9.3p392 :034?>   puts a
1.9.3p392 :035?>   end while a < b
2013-05-25 23:45:00 +0200
2013-05-26 00:00:00 +0200
2013-05-26 00:15:00 +0200
2013-05-26 00:30:00 +0200

也许这有点帮助

相关问题