How to get start or end of a day in Python

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

Get the start of a day

1
2
3
4
from datetime import datetime, time

start_of_day = datetime.combine(datetime.now(), time.min)
print(start_of_day) # 2023-02-10 00:00:00

Get the end of a day

1
2
3
4
from datetime import datetime, time

end_of_day = datetime.combine(datetime.now(), time.max)
print(start_of_day) # 2023-02-10 23:59:59.999999