How to tell how many weeks apart two datetimes are?

Discussion in 'Python' started by Roy Smith, Jan 8, 2013.

  1. Roy Smith

    Roy Smith Guest

    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?
     
    Roy Smith, Jan 8, 2013
    #1
    1. Advertising

  2. Roy Smith

    marduk Guest

    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
     
    marduk, Jan 8, 2013
    #2
    1. Advertising

  3. Roy Smith

    Ian Kelly Guest

    On Tue, Jan 8, 2013 at 2: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?


    Seems to be supported in Python 3.3, but not in 2.7.
     
    Ian Kelly, Jan 8, 2013
    #3
  4. Roy Smith

    Ian Kelly Guest

    On Tue, Jan 8, 2013 at 2:33 PM, Ian Kelly <> wrote:
    > On Tue, Jan 8, 2013 at 2: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?

    >
    > 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.
     
    Ian Kelly, Jan 8, 2013
    #4
  5. Roy Smith

    MRAB Guest

    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
     
    MRAB, Jan 8, 2013
    #5
  6. On 8 January 2013 22:50, MRAB <> 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
     
    Oscar Benjamin, Jan 8, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Two radio buttons, far apart

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Mar 2, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    448
    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
    Mar 5, 2006
  2. Mr Flibble
    Replies:
    1
    Views:
    289
    John Timney \(MVP\)
    Jun 21, 2006
  3. Stewart Midwinter

    how to calc the difference between two datetimes?

    Stewart Midwinter, May 9, 2005, in forum: Python
    Replies:
    0
    Views:
    331
    Stewart Midwinter
    May 9, 2005
  4. W. eWatson

    Difference Between Two datetimes

    W. eWatson, Dec 28, 2009, in forum: Python
    Replies:
    24
    Views:
    670
    Steve Ferg
    Jan 14, 2010
  5. mike
    Replies:
    2
    Views:
    93
    David Dorward
    Jan 28, 2005
Loading...

Share This Page