Answers for "datetime round to seconds python"

1

python datetime round to nearest hour

from datetime import datetime, timedelta

now = datetime.now()

def hour_rounder(t):
    # Rounds to nearest hour by adding a timedelta hour if minute >= 30
    return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour)
               +timedelta(hours=t.minute//30))

print(now)
print(hour_rounder(now))
Posted by: Guest on June-21-2021

Code answers related to "datetime round to seconds python"

Python Answers by Framework

Browse Popular Code Answers by Language