how.wtf

How to get start or end of a day in Python

· Thomas Taylor

In Python, datetime natively handles getting the earliest/latest moment of a given day using the combine function.

Get the start of a day

1from datetime import datetime, time
2
3start_of_day = datetime.combine(datetime.now(), time.min)
4print(start_of_day) # 2023-02-10 00:00:00

Get the end of a day

1from datetime import datetime, time
2
3end_of_day = datetime.combine(datetime.now(), time.max)
4print(end_of_day) # 2023-02-10 23:59:59.999999

#python  

Reply to this post by email ↪