22

I've RTFM and read many questions and answers here on SO regarding this, and was happily using strftime and strptime yesterday, so I would swear this should work, but it isn't....

I just want an integer. Not a "timedelta object." Not an "aware yet hashable object" (see, I RTFM). Not a tuple. Not a dictionary. Just a simple freaking integer so I can use an if statement and branch and be happy. Please bring the light of your wisdom upon this, with thanks.

Here's what I have

...
import datetime
mdate = "2010-10-05"
rdate = "2010-10-05"
mdate1 = datetime.strptime(mdate, "%Y-%m-%d")
rdate1 = datetime.strptime(rdate, "%Y-%m-%d")
delta =  datetime.timedelta.days(mdate1 - rdate1)

Here's what I get:

pmain.py:4: AttributeError: 'module' object has no attribute 'strptime'
(error hits in the 'mdate1..." line above)

And, that doesn't mean that my delta line is going to work -- please look at that one, too.

2
  • 5
    datetime.datetime.strptime Commented Apr 23, 2013 at 13:34
  • Partial credit. Next error hits for delta line: pmain.py:6: TypeError: 'member_descriptor' object is not callable Commented Apr 23, 2013 at 13:36

3 Answers 3

46

You want to get the classmethod datetime.datetime.strptime(), then take the .days attribute from the resulting timedelta:

import datetime

mdate = "2010-10-05"
rdate = "2010-10-05"
mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d").date()
rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d").date()
delta =  (mdate1 - rdate1).days

So you have the datetime module, which has a datetime.datetime class, which in turn has a datetime.datetime.strptime() method on it. I also added calls to .date() to extract just the date portion (result is a datetime.date instance); this makes dealing with timestamps that differ slightly less than a multiple of 24 hours easier.

Demo:

>>> import datetime
>>> mdate = "2010-10-05"
>>> rdate = "2010-10-05"
>>> mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d").date()
>>> rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d").date()
>>> delta =  (mdate1 - rdate1).days
>>> print delta
0
>>> type(delta)
<type 'int'>
Sign up to request clarification or add additional context in comments.

8 Comments

Perfet, Martijn - and as always you answer and explain and benefit future coders with your answers. Happy happy code now -- many thanks!
Might be better to also check seconds in case the OP wants to round to the nearest whole day.
Is the type of the variable delta an int?
@George: added verification that it is indeed an int, just for you.
@Aya: Yup, but not here, because the default datetime objects are timezone-naive (no timezone info at all, so no DST and no timezone changes either). :-)
|
5
sign1['days'] = sign1['diff'] / np.timedelta64(1, 'D')

I had the same problem and it solved by uding the above statement. I hope it helps.

Comments

-1
import datetime
mdate = "2010-11-05"
rdate = "2010-10-05"
mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d")
rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d")
delta = (mdate1 - rdate1).days

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.