Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:number': 0.07; 'subject:How': 0.09; 'rounding': 0.09; 'cc:addr:python-list': 0.10; 'assume': 0.11; "hasn't": 0.15; 'thu,': 0.15; 'boundary,': 0.16; 'did,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'rough': 0.16; 'subject:between': 0.16; 'subject:dates': 0.16; 'subject:weeks': 0.16; 'sufficient,': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'code.': 0.23; '2015': 0.23; 'this:': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'checking': 0.27; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.28; "i'm": 0.29; 'there.': 0.30; 'code': 0.31; 'skip:d 40': 0.32; 'michael': 0.33; 'adjust': 0.33; 'point,': 0.33; 'definition': 0.34; 'weeks': 0.34; 'subject:?': 0.34; 'previous': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'next': 0.35; 'skip:d 30': 0.35; 'something': 0.35; 'represent': 0.35; 'but': 0.36; 'week.': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'is,': 0.38; 'shared': 0.38; 'end': 0.39; 'pm,': 0.39; 'test': 0.39; 'sure': 0.40; 'your': 0.60; 'even': 0.61; "you'll": 0.61; "you've": 0.61; 'taking': 0.62; 'complete': 0.63; 'between': 0.65; 'day': 0.70; 'chrisa': 0.84; 'divide': 0.84; 'loose': 0.84; 'twelve': 0.84; 'to:none': 0.90; 'joel': 0.91; 'subject:find': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=h+cEUqIVwm3uFqyh7/YlJlq4gS31laPRxaYyu48pgJk=; b=G3OYnh9pJXHHcy9Epz3FODPVn5twl2f29y/R/at0X7rXwVEIsoFb4HqnWBxfk+IMHZ lO4LH36LPjDr+9hmTFNx7o0Y25ZyyqeLiYWSH4CUsDIKZgtwnpWpev32AW6wOcWrjeDy fSPCgCiVo01WrDByWsbmJ1F4UM9urvMb0Yt/PTaOfBgXLjYzv43sNVdu1p8MKCB2V8Zy Ea42R6lF1Ll+WhwhdBSGRcYn08JnN9jHwps0OaI6dIRe4pVODe+5jFrCZPKnQTHutaWy Ujp93gI5ImLDfbDEpO1Bf4XK7VtVD77K2cir3M48/WycCi0EHhYL8oso5BhPX6jskMxB Hxbg== MIME-Version: 1.0 X-Received: by 10.50.141.164 with SMTP id rp4mr32185887igb.2.1433993775527; Wed, 10 Jun 2015 20:36:15 -0700 (PDT) In-Reply-To: <5578FE53.6090906@gmail.com> References: <5578FE53.6090906@gmail.com> Date: Thu, 11 Jun 2015 13:36:15 +1000 Subject: Re: How to find number of whole weeks between dates? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433993784 news.xs4all.nl 2844 [2001:888:2000:d::a6]:40764 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92449 On Thu, Jun 11, 2015 at 1:19 PM, Michael Torrie wrote: > I think Joel had the right idea. First calculate the rough number of > weeks by taking the number of days between the date and divide by seven. > Then check to see what the start date's day of week is, and adjust the > rough week count down by one if it's not the first day of the week. I'm > not sure if you have to check the end date's day of week or not. I kind > of think checking the first one only is sufficient, but I could be > wrong. You'll have to code it up and test it, which I assume you've > been doing up to this point, even though you haven't shared any code. Alternatively, you could start by rounding the start date up to the next week boundary, then round the end date down to the previous week boundary, and then calculate from there. Something like this: >>> start = datetime.date(2015, 1, 4) >>> end = datetime.date(2015, 4, 2) >>> start += datetime.timedelta(7-start.isoweekday()) >>> end -= datetime.timedelta(end.isoweekday() % 7) Now both dates represent Sundays. If either already did, it hasn't been changed. >>> (end - start).days//7 12 There are twelve complete Sunday-to-Sunday weeks (plus any loose days either end) between the original dates. Depending on your definition of "complete week", you may need to adjust this code some. ChrisA