How to tell how many weeks apart two datetimes are?

R

Roy Smith

How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed. Is there some fundamental
reason why timedelta division not supported?
 
M

marduk

How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed. Is there some fundamental
reason why timedelta division not supported?

It works for python 3(.2):
1332.0
 
I

Ian Kelly

How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed. Is there some fundamental
reason why timedelta division not supported?

Seems to be supported in Python 3.3, but not in 2.7.
 
I

Ian Kelly

Seems to be supported in Python 3.3, but not in 2.7.
From the docs:

Changed in version 3.2: Floor division and true division of a
timedelta object by another timedelta object are now supported, as are
remainder operations and the divmod() function. True division and
multiplication of a timedelta object by a float object are now
supported.
 
M

MRAB

How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed. Is there some fundamental
reason why timedelta division not supported?
Try this:

weeks = (t2 - t1).days / 7
 
O

Oscar Benjamin

Try this:

weeks = (t2 - t1).days / 7

You beat me to it...

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.False


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top