snippetpythonCritical
Convert date to datetime in Python
Viewed 0 times
datetimedateconvertpython
Problem
Is there a built-in method for converting a
Do I really have to manually call
date to a datetime in Python, for example getting the datetime for the midnight of the given date? The opposite conversion is easy: datetime has a .date() method.Do I really have to manually call
datetime(d.year, d.month, d.day)?Solution
You can use
datetime.combine(date, time); for the time, you create a datetime.time object initialized to midnight.from datetime import date
from datetime import datetime
dt = datetime.combine(date.today(), datetime.min.time())Code Snippets
from datetime import date
from datetime import datetime
dt = datetime.combine(date.today(), datetime.min.time())Context
Stack Overflow Q#1937622, score: 1326
Revisions (0)
No revisions yet.