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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Todd Curry
  • 1,045
  • 1
  • 10
  • 23

3 Answers3

45

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'>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Perfet, Martijn - and as always you answer and explain and benefit future coders with your answers. Happy happy code now -- many thanks! – Todd Curry Apr 23 '13 at 13:41
  • Might be better to also check seconds in case the OP wants to round to the nearest whole day. – Aya Apr 23 '13 at 13:41
  • Is the type of the variable `delta` an `int`? – George Apr 23 '13 at 13:41
  • 2
    @George: added verification that it is indeed an `int`, just for you. – Martijn Pieters Apr 23 '13 at 13:42
  • @Aya: No, this *is* whole days already. That is what the `.days()` calls are for. – Martijn Pieters Apr 23 '13 at 13:42
  • @MartijnPieters Oops. Forgot the `strptime` was only on a date, although there are some [unusual cases](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result) where you might not get back a `timedelta` where `seconds` is zero. – Aya Apr 23 '13 at 13:58
  • 1
    @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). :-) – Martijn Pieters Apr 23 '13 at 14:11
  • @MartijnPieters Okay. You win this round. ;-) – Aya Apr 23 '13 at 14:12
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.

Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24
shilpa.rpns
  • 51
  • 1
  • 3
-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
PLABON DATTA
  • 167
  • 1
  • 4