Round down datetime to previous hour

前端 未结 2 1732
执笔经年
执笔经年 2021-01-03 21:34

How to round down datetime to previous hour? for example:

print datetime.now().replace(microsecond=0)
>> 2017-01-11 13:26:12.0

round

2条回答
  •  旧巷少年郎
    2021-01-03 21:54

    from datetime import datetime, timedelta
    
    n = datetime.now() - timedelta(hours=1)
    new_date = datetime(year=n.year, month=n.month, day=n.day, hour=n.hour)
    

提交回复
热议问题