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


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

How to find number of whole weeks between dates?

Started bySebastian M Cheung <minscheung@googlemail.com>
First post2015-06-10 10:05 -0700
Last post2015-06-10 21:35 -0600
Articles 15 — 8 participants

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


Contents

  How to find number of whole weeks between dates? Sebastian M Cheung <minscheung@googlemail.com> - 2015-06-10 10:05 -0700
    Re: How to find number of whole weeks between dates? Marko Rauhamaa <marko@pacujo.net> - 2015-06-10 20:38 +0300
      Re: How to find number of whole weeks between dates? Marko Rauhamaa <marko@pacujo.net> - 2015-06-10 20:43 +0300
      Re: How to find number of whole weeks between dates? Laura Creighton <lac@openend.se> - 2015-06-10 19:50 +0200
      Re: How to find number of whole weeks between dates? Joel Goldstick <joel.goldstick@gmail.com> - 2015-06-10 14:07 -0400
      Re: How to find number of whole weeks between dates? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-10 20:48 +0100
    Re: How to find number of whole weeks between dates? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-10 11:42 -0600
    Re: How to find number of whole weeks between dates? Sebastian M Cheung <minscheung@googlemail.com> - 2015-06-10 13:11 -0700
      Re: How to find number of whole weeks between dates? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-10 21:26 +0100
      Re: How to find number of whole weeks between dates? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-10 14:33 -0600
      Re: How to find number of whole weeks between dates? Michael Torrie <torriem@gmail.com> - 2015-06-10 21:19 -0600
      Re: How to find number of whole weeks between dates? Chris Angelico <rosuav@gmail.com> - 2015-06-11 13:36 +1000
      Re: How to find number of whole weeks between dates? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-10 21:45 -0600
    Re: How to find number of whole weeks between dates? Sebastian M Cheung <minscheung@googlemail.com> - 2015-06-10 19:01 -0700
      Re: How to find number of whole weeks between dates? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-10 21:35 -0600

#92409 — How to find number of whole weeks between dates?

FromSebastian M Cheung <minscheung@googlemail.com>
Date2015-06-10 10:05 -0700
SubjectHow to find number of whole weeks between dates?
Message-ID<a2056385-f99b-4691-9a33-5def63216e9c@googlegroups.com>
Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total

[toc] | [next] | [standalone]


#92413

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-06-10 20:38 +0300
Message-ID<877frbwjik.fsf@elektro.pacujo.net>
In reply to#92409
Sebastian M Cheung <minscheung@googlemail.com>:

> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and
> May would be 5th, 12th and 19th. So expecting 7 whole weeks in total

This program gives you the number of days between two dates given in the
YYYY-MM-DD format:

========================================================================
#!/usr/bin/env python3

import sys

def gregorian_day_count(isodate):
    year, month, day = map(int, isodate.split('-'))
    a, b = divmod(12 * year + month - 3, 12)
    return (a * 365 + (a >> 2) - (a * 1311 >> 17) + (a * 1311 >> 19) +
            + (31306 * b + 722 >> 10))

def main():
    print(gregorian_day_count(sys.argv[2]) - gregorian_day_count(sys.argv[1]))

if __name__ == '__main__':
    main()
========================================================================

Divide the number by 7 and you have your answer.


Marko

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


#92414

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-06-10 20:43 +0300
Message-ID<87381zwjac.fsf@elektro.pacujo.net>
In reply to#92413
Marko Rauhamaa <marko@pacujo.net>:

> This program gives you the number of days between two dates given in the
> YYYY-MM-DD format:

Sorry, couldn't resist.

It still does work, though.


Marko

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


#92416

FromLaura Creighton <lac@openend.se>
Date2015-06-10 19:50 +0200
Message-ID<mailman.352.1433958641.13271.python-list@python.org>
In reply to#92413
In a message of Wed, 10 Jun 2015 20:38:59 +0300, Marko Rauhamaa writes:
>Divide the number by 7 and you have your answer.
>

I am not sure that is what he wants -- If he gives us a start of Tuesday the
9th of June 2015 (yesterday) and an end of Thursday the 25th of June, that's
16 days.  But there is only one Monday-Friday week in there, the 14th-19th.

So if the OP wants an answer of 1 for such data, he may be interested in
the python calendar module https://docs.python.org/2/library/calendar.html

Laura

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


#92419

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2015-06-10 14:07 -0400
Message-ID<mailman.355.1433959670.13271.python-list@python.org>
In reply to#92413
On Wed, Jun 10, 2015 at 1:50 PM, Laura Creighton <lac@openend.se> wrote:
> In a message of Wed, 10 Jun 2015 20:38:59 +0300, Marko Rauhamaa writes:
>>Divide the number by 7 and you have your answer.
>>
>
> I am not sure that is what he wants -- If he gives us a start of Tuesday the
> 9th of June 2015 (yesterday) and an end of Thursday the 25th of June, that's
> 16 days.  But there is only one Monday-Friday week in there, the 14th-19th.
>
> So if the OP wants an answer of 1 for such data, he may be interested in
> the python calendar module https://docs.python.org/2/library/calendar.html
>
> Laura
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Find the number of weeks with the above method, then

>>> import datetime
end_date = datetime.datetime(2012, 3, 23)  // whatever your end date is
if end_date.weekday() != 5:
    number_of_complete _weeks -= 1

weekday returns 0 for monday, so 5 for Saturday


-- 
Joel Goldstick
http://joelgoldstick.com

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


#92422

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-06-10 20:48 +0100
Message-ID<mailman.358.1433965732.13271.python-list@python.org>
In reply to#92413
On 10/06/2015 18:50, Laura Creighton wrote:
> In a message of Wed, 10 Jun 2015 20:38:59 +0300, Marko Rauhamaa writes:
>> Divide the number by 7 and you have your answer.
>>
>
> I am not sure that is what he wants -- If he gives us a start of Tuesday the
> 9th of June 2015 (yesterday) and an end of Thursday the 25th of June, that's
> 16 days.  But there is only one Monday-Friday week in there, the 14th-19th.
>
> So if the OP wants an answer of 1 for such data, he may be interested in
> the python calendar module https://docs.python.org/2/library/calendar.html
>
> Laura
>
>

For those who wish to move into the 21st century the link is 
https://docs.python.org/3/library/calendar.html

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


#92415

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-06-10 11:42 -0600
Message-ID<mailman.351.1433958601.13271.python-list@python.org>
In reply to#92409
On Wed, Jun 10, 2015 at 11:05 AM, Sebastian M Cheung via Python-list
<python-list@python.org> wrote:
> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total

>>> from datetime import date
>>> d1 = date(2014, 4, 7)
>>> d2 = date(2014, 5, 19)
>>> d2 - d1
datetime.timedelta(42)
>>> (d2 - d1).days
42
>>> (d2 - d1).days // 7
6

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


#92424

FromSebastian M Cheung <minscheung@googlemail.com>
Date2015-06-10 13:11 -0700
Message-ID<a650c18e-fa33-4139-bc68-e14b08273a8d@googlegroups.com>
In reply to#92409
On Wednesday, June 10, 2015 at 6:06:09 PM UTC+1, Sebastian M Cheung wrote:
> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total

What I mean is given two dates I want to find WHOLE weeks, so if given the 2014 calendar and function has two inputs (4th and 5th month) then 7th, 14th, 21st and 28th from April with 28th April week carrying into May, and then 5th, 12th and 19th May to give total of 7 whole weeks, because 26th May is not a whole week and will not be counted.

Hope thats clear.

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


#92425

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-06-10 21:26 +0100
Message-ID<mailman.359.1433967979.13271.python-list@python.org>
In reply to#92424
On 10/06/2015 21:11, Sebastian M Cheung via Python-list wrote:
> On Wednesday, June 10, 2015 at 6:06:09 PM UTC+1, Sebastian M Cheung wrote:
>> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total
>
> What I mean is given two dates I want to find WHOLE weeks, so if given the 2014 calendar and function has two inputs (4th and 5th month) then 7th, 14th, 21st and 28th from April with 28th April week carrying into May, and then 5th, 12th and 19th May to give total of 7 whole weeks, because 26th May is not a whole week and will not be counted.
>
> Hope thats clear.
>

If you'd be kind enough to show the code that you've written and the 
precise reasons(s) that it doesn't work then we'll be delighted to point 
you in the right direction.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


#92426

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-06-10 14:33 -0600
Message-ID<mailman.360.1433968445.13271.python-list@python.org>
In reply to#92424
On Wed, Jun 10, 2015 at 2:11 PM, Sebastian M Cheung via Python-list
<python-list@python.org> wrote:
> On Wednesday, June 10, 2015 at 6:06:09 PM UTC+1, Sebastian M Cheung wrote:
>> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total
>
> What I mean is given two dates I want to find WHOLE weeks, so if given the 2014 calendar and function has two inputs (4th and 5th month) then 7th, 14th, 21st and 28th from April with 28th April week carrying into May, and then 5th, 12th and 19th May to give total of 7 whole weeks, because 26th May is not a whole week and will not be counted.

So the two "dates" being passed are actually months? The calendar
module already suggested should be useful for this.

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


#92445

FromMichael Torrie <torriem@gmail.com>
Date2015-06-10 21:19 -0600
Message-ID<mailman.376.1433992800.13271.python-list@python.org>
In reply to#92424
On 06/10/2015 02:11 PM, Sebastian M Cheung via Python-list wrote:
> On Wednesday, June 10, 2015 at 6:06:09 PM UTC+1, Sebastian M Cheung wrote:
>> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total
> 
> What I mean is given two dates I want to find WHOLE weeks, so if given the 2014 calendar and function has two inputs (4th and 5th month) then 7th, 14th, 21st and 28th from April with 28th April week carrying into May, and then 5th, 12th and 19th May to give total of 7 whole weeks, because 26th May is not a whole week and will not be counted.
> 
> Hope thats clear.

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.

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


#92449

FromChris Angelico <rosuav@gmail.com>
Date2015-06-11 13:36 +1000
Message-ID<mailman.378.1433993784.13271.python-list@python.org>
In reply to#92424
On Thu, Jun 11, 2015 at 1:19 PM, Michael Torrie <torriem@gmail.com> 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

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


#92452

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-06-10 21:45 -0600
Message-ID<mailman.381.1433994374.13271.python-list@python.org>
In reply to#92424
On Wed, Jun 10, 2015 at 9:19 PM, Michael Torrie <torriem@gmail.com> wrote:
> On 06/10/2015 02:11 PM, Sebastian M Cheung via Python-list wrote:
>> On Wednesday, June 10, 2015 at 6:06:09 PM UTC+1, Sebastian M Cheung wrote:
>>> Say in 2014 April to May whole weeks would be 7th, 14th 28th April and  May would be 5th, 12th and 19th. So expecting 7 whole weeks in total
>>
>> What I mean is given two dates I want to find WHOLE weeks, so if given the 2014 calendar and function has two inputs (4th and 5th month) then 7th, 14th, 21st and 28th from April with 28th April week carrying into May, and then 5th, 12th and 19th May to give total of 7 whole weeks, because 26th May is not a whole week and will not be counted.
>>
>> Hope thats clear.
>
> 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.

I don't think the logic is quite right. Consider:

>>> cal = calendar.TextCalendar()
>>> print(cal.formatmonth(2014, 6))
     June 2014
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

>>> date(2014, 7, 1) - date(2014, 6, 1)
datetime.timedelta(30)
>>> _.days // 7 - 1
3

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


#92441

FromSebastian M Cheung <minscheung@googlemail.com>
Date2015-06-10 19:01 -0700
Message-ID<c62196d7-d1fb-4f0d-bb22-090aac862145@googlegroups.com>
In reply to#92409
yes just whole weeks given any two months, I did looked into calendar module but couldn't find specifically what i need.

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


#92450

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-06-10 21:35 -0600
Message-ID<mailman.379.1433993802.13271.python-list@python.org>
In reply to#92441
On Wed, Jun 10, 2015 at 8:01 PM, Sebastian M Cheung via Python-list
<python-list@python.org> wrote:
> yes just whole weeks given any two months, I did looked into calendar module but couldn't find specifically what i need.

>>> cal.monthdays2calendar(2014, 4) + cal.monthdays2calendar(2014, 5)
[[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0),
(8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15,
1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1),
(23, 2), (24, 3), (25, 4), (26, 5), (27, 6)], [(28, 0), (29, 1), (30,
2), (0, 3), (0, 4), (0, 5), (0, 6)], [(0, 0), (0, 1), (0, 2), (1, 3),
(2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10,
5), (11, 6)], [(12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5),
(18, 6)], [(19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25,
6)], [(26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (31, 5), (0, 6)]]

You just need to:

1) Trim the first and last weeks off since they contain invalid dates.
2) Merge the overlapping last week of April and first week of May.
3) Count the resulting number of weeks in the list.

Alternatively, the dateutil.rrule module could probably be used to do
this fairly easily, but it's a third-party module and not part of the
standard library.

https://labix.org/python-dateutil

[toc] | [prev] | [standalone]


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


csiph-web