Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36451 > unrolled thread

How to tell how many weeks apart two datetimes are?

Started byroy@panix.com (Roy Smith)
First post2013-01-08 16:22 -0500
Last post2013-01-08 22:54 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  How to tell how many weeks apart two datetimes are? roy@panix.com (Roy Smith) - 2013-01-08 16:22 -0500
    Re: How to tell how many weeks apart two datetimes are? marduk <marduk@python.net> - 2013-01-08 16:31 -0500
    Re: How to tell how many weeks apart two datetimes are? Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-08 14:33 -0700
    Re: How to tell how many weeks apart two datetimes are? Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-08 14:36 -0700
    Re: How to tell how many weeks apart two datetimes are? MRAB <python@mrabarnett.plus.com> - 2013-01-08 22:50 +0000
    Re: How to tell how many weeks apart two datetimes are? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-08 22:54 +0000

#36451 — How to tell how many weeks apart two datetimes are?

Fromroy@panix.com (Roy Smith)
Date2013-01-08 16:22 -0500
SubjectHow to tell how many weeks apart two datetimes are?
Message-ID<kci2m1$fp4$1@panix2.panix.com>
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?

[toc] | [next] | [standalone]


#36452

Frommarduk <marduk@python.net>
Date2013-01-08 16:31 -0500
Message-ID<mailman.291.1357680711.2939.python-list@python.org>
In reply to#36451

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

[toc] | [prev] | [next] | [standalone]


#36453

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-01-08 14:33 -0700
Message-ID<mailman.292.1357680863.2939.python-list@python.org>
In reply to#36451
On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith <roy@panix.com> 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.

[toc] | [prev] | [next] | [standalone]


#36454

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-01-08 14:36 -0700
Message-ID<mailman.293.1357681002.2939.python-list@python.org>
In reply to#36451
On Tue, Jan 8, 2013 at 2:33 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith <roy@panix.com> 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.

[toc] | [prev] | [next] | [standalone]


#36455

FromMRAB <python@mrabarnett.plus.com>
Date2013-01-08 22:50 +0000
Message-ID<mailman.294.1357685430.2939.python-list@python.org>
In reply to#36451
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

[toc] | [prev] | [next] | [standalone]


#36456

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-01-08 22:54 +0000
Message-ID<mailman.295.1357685662.2939.python-list@python.org>
In reply to#36451
On 8 January 2013 22:50, MRAB <python@mrabarnett.plus.com> 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=8)
>>> (dt2 - dt1) / 7 > datetime.timedelta(days=14)
False


Oscar

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web