简体   繁体   English

如何在Calendar模块Python中将默认的第一周工作日数更改为0

[英]How to change the default first week day number to 0 in Calendar module Python

I have implemented a logic in my code where I need to calculate the date/day considering Sunday as the starting day of week. 我在我的代码中实现了一个逻辑,我需要计算日期/日,将星期日视为星期的开始日期。 When I am using this code 当我使用此代码时

import calendar
day_number = calendar.weekday(2014, 01, 19)

Its returning day_number as 6, but I need it should return as 0, since 19 Jan 2014 is a sunday. 它返回day_number为6,但我需要它返回0,因为2014年1月19日是星期天。

I have also tried to use this: 我也试过用这个:

calendar.setfirstweekday(calendar.SUNDAY)

But still the sunday number remains as 6. Is there any setting using which we can set the sunday number as 0, and the week numbers should appear as below 但是星期日的数字仍然是6.是否有任何设置我们可以将星期日数字设置为0,周数应该如下所示

'sun': 0, 
'mon': 1,
'tue': 2,
'wed': 3,
'thu': 4,
'fri': 5,
'sat': 6,

setfirstweekday affects only display functions like prmonth : setfirstweekday仅影响prmonth等显示函数:

>>> calendar.prmonth(2014, 1)
    January 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 31
>>> calendar.setfirstweekday(calendar.SUNDAY)
>>> calendar.prmonth(2014, 1)
    January 2014
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

How about using modulo? 使用modulo怎么样?

>>> (calendar.weekday(2014, 1, 19) + 1) % 7
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM