用Python方式添加datetime.date和datetime.time对象

问题:用Python方式添加datetime.date和datetime.time对象

我有两个代表同一事件实例的对象-一个保存日期,另一个保存该事件的时间,我想创建一个datetime对象。

由于不能简单地添加日期和时间对象(以下调用失败):

 datetime.date(2011, 01, 01) + datetime.time(10, 23)

I have two objects that represent the same event instance — one holds the date, the other the time of this event, and I want to create a datetime object.

Since one can’t simply add date and time objects (following call fails):

 datetime.date(2011, 01, 01) + datetime.time(10, 23)

回答 0

python docs中

import datetime
datetime.datetime.combine(datetime.date(2011, 1, 1), 
                          datetime.time(10, 23))

退货

datetime.datetime(2011, 1, 1, 10, 23)

It’s in the python docs.

import datetime
datetime.datetime.combine(datetime.date(2011, 1, 1), 
                          datetime.time(10, 23))

returns

datetime.datetime(2011, 1, 1, 10, 23)