Daniel
Daniel

Reputation: 1664

How to add datetime.time to datetime.datetime

I have both a Python datetime.datetime object and a datatime.time object and am looking for a way to add the time to the datetime object.

A simple addition doesn't seem to be supported. The time is essentially representing a duration.

Upvotes: 2

Views: 1195

Answers (1)

Félix Herbinet
Félix Herbinet

Reputation: 277

Hi let's suppose that you have an instance t of class datetime.time and an instance dt of class datetime.datetime:

new_dt = dt + datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond)

Upvotes: 3

Related Questions