Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find current time interval in python?

How can I find the nearest 15 (or 10) minute interval in python ? e.g.

>>> datetime.datetime.now()
datetime.datetime(2011, 2, 22, 15, 43, 18, 424873)

I'd like the current 15 minute interval (15:30-15:44) so I'd like to transform the above datetime to a

datetime.datetime(2011, 2, 22, 15, 30, 00, 00)
like image 415
Habalusa Avatar asked Feb 22 '11 14:02

Habalusa


1 Answers

Quite the easiest way to me:

from datetime import datetime, timedelta
now = datetime.now()
now = now - timedelta(minutes = now.minute % 15, seconds = now.second, microseconds = now.microsecond )

Hope that'll help.

like image 172
Gandi Avatar answered Sep 21 '22 16:09

Gandi