Edward J. Stembler
Edward J. Stembler

Reputation: 2042

How do I get a range or array for every minute in a day in Ruby 1.9.3?

I know there is the Date#step method, however it wants days for steps. I need a range or array for every minute in a given day (1440 entries).

What's the best, and most effeicient way to do this in Ruby 1.9.3?

Ultimately, I'm going to format the output to be used like this:

00:00:00
00:01:00
00:02:00
...
23:59:00

Upvotes: 1

Views: 1236

Answers (2)

steenslag
steenslag

Reputation: 80065

You can step anyway:

require 'date'
today = Date.today.to_datetime
tomorrow = today+1
min = 1.0/(24*60)
today.step(tomorrow, min){|d| p d.strftime("%H:%M:%S")}

Upvotes: 0

rfunduk
rfunduk

Reputation: 30442

This might get you started:

0.upto((60 * 24) - 1).each { |m| puts "%02d:%02d:00" % [m / 60, m % 60] }

Upvotes: 2

Related Questions