pd2
Posts: 53
Joined: Sun Jun 05, 2016 11:42 am

datetime combine driving me mad

Thu Oct 06, 2016 7:55 pm

There is obviously something I don't understand but I am a beginner.
I have this code:

Code: Select all

import datetime 
from datetime import datetime, date, time, timedelta
midnight=datetime.combine(datetime.now(),datetime.time(0,0,0))
I get the error "TypeError: descriptor 'time' requires a datetime.datetime object but received a 'int'.
I looks to me that I have used datetime but...
I tried various combinations but am getting no-where.

BMS Doug
Posts: 3959
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: datetime combine driving me mad

Thu Oct 06, 2016 8:21 pm

pd2 wrote:There is obviously something I don't understand but I am a beginner.
I have this code:

Code: Select all

import datetime 
from datetime import datetime, date, time, timedelta
midnight=datetime.combine(datetime.now(),datetime.time(0,0,0))
I get the error "TypeError: descriptor 'time' requires a datetime.datetime object but received a 'int'.
I looks to me that I have used datetime but...
I tried various combinations but am getting no-where.
I've had a quick read of the datetime.combine action and it seems to me that you need to combine a time and a date.

Try this:

Code: Select all

import datetime 
from datetime import datetime, date, time, timedelta
midnight = datetime.datetime.combine(datetime.date(), datetime.time(0,0,0))
Doug.
Building Management Systems Engineer.

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: datetime combine driving me mad

Thu Oct 06, 2016 8:40 pm

You said

Code: Select all

import datetime 
from datetime import datetime, date, time, timedelta
midnight=datetime.combine(datetime.now(),datetime.time(0,0,0))
The problem here is that you are importing datetime which has classes datetime and time.
Then you import datetime (from datetime) and time (from datetime)

But datetime.datetime also has a class time (datetime.datetime.time)

In other words, your namespace is ambiguous. I bet that time(0,0,0) will work.
Personally, I would just import datetime, and skip the 'from' clause.

Code: Select all

import datetime
datetime.datetime.combine(datetime.datetime.now(),datetime.time(0,0,0))

Return to “Python”