-1

可能重复:
如何迭代天

我有一个循环 days.this 的代码来制作树叶。我希望出勤时的列登录和注销将在开始日期开始并在结束日期结束时自动填充。

**exp I input :**
startdate: 2012-11-08 01:30:00
enddate: 2012-11-10 01:30:00
**i want output like this:** 

2012-11-08 01:30:00
2012-11-09 01:30:00
2012-11-010 01:30:00

 for i in 0..((@leafe.enddate - @leafe.startdate).to_i)
                 @attendance = Attendance.new

                 @attendance.signin = '2012-11-08 01:30:00' #value must change automatically
                 @attendance.signout = '2012-11-08 10:30:00'#value must change automatically
                 @attendance.user_id = @leafe.user_id
                 @attendance.save
            end

对于你的回答,我说谢谢

4

1 回答 1

1

我真的很难理解你的问题描述。我只是猜测您想迭代一系列日期。

start_date = Date.new(2012, 11, 8)
end_date   = Date.new(2012, 11, 23)

(start_date .. end_date).each do |date|
  …
  # Do something with date here
  …
end

请注意,我在Date这里使用该类。您可能需要先使用require 'date'. 您应该尝试使用代表您正在使用的事物的特定类。在完全面向对象的语言(如 Ruby)中,使用 anInteger或 aString来避免许多强大的特定功能。Date

由于您不仅有日期作为输入,还有完整的 ISO-8601 时间规范,您可能希望使用类似这样的东西来仅提取 Date 对象:

Time.parse('2012-11-20 12:30:45').to_date

在这里,您可能需要使用require 'time'.

于 2012-11-08T06:24:23.887 回答