Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to tell how many weeks apart two datetimes are?

Reply
Thread Tools

How to tell how many weeks apart two datetimes are?

 
 
Roy Smith
Guest
Posts: n/a
 
      01-08-2013
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?
 
Reply With Quote
 
 
 
 
marduk
Guest
Posts: n/a
 
      01-08-2013


On Tue, Jan 8, 2013, at 04:22 PM, Roy Smith wrote:
> 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?
> --
> http://mail.python.org/mailman/listinfo/python-list


It works for python 3(.2):

>>> x = datetime.timedelta(days=666)
>>> week = datetime.timedelta(days=7)
>>> x / week

95.14285714285714
>>> halfday = datetime.timedelta(hours=12)
>>> x / halfday

1332.0
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      01-08-2013
On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      01-08-2013
On Tue, Jan 8, 2013 at 2:33 PM, Ian Kelly <(E-Mail Removed)> wrote:
> On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith <(E-Mail Removed)> wrote:
>> 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.


>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.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-08-2013
On 2013-01-08 21:22, Roy Smith wrote:
> 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

 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      01-08-2013
On 8 January 2013 22:50, MRAB <(E-Mail Removed)> wrote:
> On 2013-01-08 21:22, Roy Smith wrote:
>>
>> 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


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.
>>> import datetime
>>> dt1 = datetime.datetime.now()
>>> dt2 = dt1 - datetime.timedelta(days=
>>> (dt2 - dt1) / 7 > datetime.timedelta(days=14)

False


Oscar
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Difference Between Two datetimes W. eWatson Python 24 01-14-2010 06:08 PM
how to calc the difference between two datetimes? Stewart Midwinter Python 0 05-09-2005 01:06 AM
How do I tell links apart in JavaScript (the DOM) mike Javascript 2 01-28-2005 09:56 PM
Circular vs Linear Polarizer - How do you tell them apart? Nick Lamendola Digital Photography 4 07-25-2004 01:56 PM



Advertisments