8

Let say I have datetime objects. I would like them rounded to the nearest preceding quarter hour:

2014-07-18T14:23:12 --> 2014-07-18T14:15:00
2014-07-18T14:14:59 --> 2014-07-18T14:00:00
2014-07-18T00:00:00 --> 2014-07-18T00:00:00

Etc.

tadasajon
  • 14,276
  • 29
  • 92
  • 144

1 Answers1

10
rounded_qtr_hour = lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,
                                                15*(dt.minute // 15))

Basically, you make a new object (modifying the old one is a less functional approach), with equivalent year, month, day, hour, and round the minute down to the last 15 minute interval (// is floor division).

metaperture
  • 2,393
  • 1
  • 18
  • 19