I have 2017-07-21 as a'datetime.date' object and
15:30:00 as a datetime.time' object
How do I combine the two to get
2017-07-21-15:30:00?
I have 2017-07-21 as a'datetime.date' object and
15:30:00 as a datetime.time' object
How do I combine the two to get
2017-07-21-15:30:00?
classmethod
datetime.combine(
date, time, tzinfo=self.tzinfo)Return a new
datetime
object whose date components are equal to the givendate
object’s, and whose time components are equal to the giventime
object’s.
Use datetime.combine
from datetime
.
import datetime
d = datetime.date(2017, 07, 21)
t = datetime.time(15, 30, 0)
dt = datetime.datetime.combine(d, t)
>>> from datetime import datetime, date, time
>>> d = date(2017, 7, 21)
>>> t = time(15, 30, 0)
>>> newDate = datetime.combine(d, t)
>>> newDate
datetime.datetime(2017, 7, 21, 15, 30)
>>> newDate.strftime(%Y-%m-%d-%H:%M:%S)
'2017-07-21-15:30:00'
You can use the datetime.combine
method as above. More about info here