Is there an easy way to round a Time down to the nearest 15 minutes?
This is what I’m currently doing. Is there an easier way to do it?
t = Time.new rounded_t = Time.local(t.year, t.month, t.day, t.hour, t.min/15*15)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You said ’round down’, so I’m not sure if you’re actually looking for the round or the floor, but here’s the code to do both. I think something like this reads really well if you add
round_offandfloormethods to the Time class. The added benefit is that you can more easily round by any time partition.Note: ActiveSupport was only necessary for the pretty
15.minutesargument. If you don’t want that dependency, use15 * 60instead.