How to find number of whole weeks between dates?

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Sebastian M Cheung
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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Marko Rauhamaa
Sebastian M Cheung <minscheung at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Ian Kelly-2
In reply to this post by Sebastian M Cheung
On Wed, Jun 10, 2015 at 11:05 AM, Sebastian M Cheung via Python-list
<python-list at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Marko Rauhamaa
In reply to this post by Marko Rauhamaa
Marko Rauhamaa <marko at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Laura Creighton-2
In reply to this post by Marko Rauhamaa
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



Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Joel Goldstick-2
On Wed, Jun 10, 2015 at 1:50 PM, Laura Creighton <lac at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Mark Lawrence
In reply to this post by Laura Creighton-2
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


Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Sebastian M Cheung
In reply to this post by Sebastian M Cheung
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.

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Mark Lawrence
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


Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Ian Kelly-2
In reply to this post by Sebastian M Cheung
On Wed, Jun 10, 2015 at 2:11 PM, Sebastian M Cheung via Python-list
<python-list at 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.

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Sebastian M Cheung
In reply to this post by Sebastian M Cheung
yes just whole weeks given any two months, I did looked into calendar module but couldn't find specifically what i need.

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Michael Torrie
In reply to this post by Sebastian M Cheung
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.

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Ian Kelly-2
In reply to this post by Sebastian M Cheung
On Wed, Jun 10, 2015 at 8:01 PM, Sebastian M Cheung via Python-list
<python-list at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Chris Angelico
In reply to this post by Michael Torrie
On Thu, Jun 11, 2015 at 1:19 PM, Michael Torrie <torriem at 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

Reply | Threaded
Open this post in threaded view
|

How to find number of whole weeks between dates?

Ian Kelly-2
In reply to this post by Michael Torrie
On Wed, Jun 10, 2015 at 9:19 PM, Michael Torrie <torriem at 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