python - Adding timedelta object to datetime -
my timedelta object looks this: txdelta = 00:30:00
. want add datetime object consistently isn't working:
from datetime import datetime, date, time, timedelta localdt = datetime.combine(datetime.strptime('2015-06-18', '%y-%m-%d').date(), (23:35:02+timedelta(txdelta)).time())
note 23:35:02
datetime object. error message:
typeerror: unsupported type timedelta days component: datetime.timedelta
what doing wrong?
the way create time
object strange. advice declare way if you're not used it:
txdelta = timedelta(minutes=30) tdelta = time(hour=1, minute=35, second=2)
if got tried combine date
, time
, timedelta
. full code below should trick:
from datetime import datetime, date, time, timedelta txdelta = timedelta(minutes=30) tdelta = time(hour=1, minute=35, second=2) localdt = datetime.combine(datetime.strptime('2015-06-18', '%y-%m-%d').date(), tdelta) + txdelta print(localdt)
basically, combine datetime object time one, , add timedelta object afterwards.
the output is:
2015-06-18 02:05:02
Comments
Post a Comment