To convert date to datetime?

Asked 2 years ago, Updated 2 years ago, 117 views

datetime can be changed to time type using datetime.date()

Is there a Python built-in function that replaces date with datetime? I know it's called datetime(date.year, date.month, date.day).

What I want is that if you call it dateToDatetime(date), the datetime type object will be returned. For the time part, it's just 00:00, just at 24:00.

python datetime date

2022-09-22 10:37

1 Answers

Write datetime.combine(date, time). For the time parameter, hand over the datetime.time object initialized at 24 o'clock sharp.

Use Case:

from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())

*datetime.min.time() is 0001-01-0100:00:00


2022-09-22 10:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.