[Tutor] how to get the weeks of a month

Mark Lawrence breamoreboy at gmail.com
Mon Mar 4 22:58:25 EST 2019


On 04/03/2019 22:12, john fabiani wrote:
> 
> 
> On 3/4/19 1:35 PM, David Rock wrote:
>>> On Mar 4, 2019, at 15:28, john fabiani <johnf at jfcomputer.com> wrote:
>>>
>>> I knew there was a simple why to get it done!  But where is it off my 
>>> a day?
>>>
>> comparing
>>
>> $ cal
>>       March 2019
>> Su Mo Tu We Th Fr Sa
>>                  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
>> 31
>>
>> to
>>
>> import calendar as cal
>> cal.monthcalendar(2019,3)
>> [[0, 0, 0, 0, 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, 31]]
>>
>> I see the first element of the array is
>> [0, 0, 0, 0, 1, 2, 3]
>>
>> where I would have expected
>> [0, 0, 0, 0, 0, 1, 2]
>>
>> Which I’m sure is just a question of “defining the start of the week” 
>> properly, but if you just took it as-is, Mar 1 would be Thursday, not 
>> Friday if you translated literally.
>>
>>
>>>> David Rock
>> david at graniteweb.com
> My understanding is - a week starts on Monday and ends on Sunday.

 From 
https://docs.python.org/3/library/calendar.html#calendar.setfirstweekday

calendar.setfirstweekday(weekday)

     Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The 
values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and 
SUNDAY are provided for convenience. For example, to set the first 
weekday to Sunday:

     import calendar
     calendar.setfirstweekday(calendar.SUNDAY)

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

Mark Lawrence



More information about the Tutor mailing list