I have the following code below.
I would like to roundup TIME to the nearest 30 minutes in the hour. For example: 12:00PM or 12:30PM and so on.
EASTE
to round forward you can use :
#!/usr/bin/env python3
from datetime import datetime, timedelta
def ceil_dt(dt, delta):
return dt + (datetime.min - dt) % delta
now = datetime.now()
print(now)
print(ceil_dt(now, timedelta(minutes=30)))
To round back to the nearest 30th minute
def rounded_to_the_last_30th_minute_epoch():
now = datetime.now()
rounded = now - (now - datetime.min) % timedelta(minutes=30)
return rounded