Python 如何计算提供了日期和月份的星期几

Python 如何计算提供了日期和月份的星期几,python,python-3.x,Python,Python 3.x,该计划严格规定于2014年实施。然而,我想知道我是否朝着正确的方向前进。这就是我到目前为止所做的: def day(d,m): # Function for determining day name for a given date. """Where m is an integer from 1 through 12 expressing a month, and d is an integer from 1 through 31 expressing the day-par

该计划严格规定于2014年实施。然而,我想知道我是否朝着正确的方向前进。这就是我到目前为止所做的:

def day(d,m): # Function for determining day name for a given date.
    """Where m is an integer from 1 through 12 expressing a month, and d is an integer from 
    1 through 31 expressing the day-part of a date in 2014."""

    day = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
    weekday = (d + (2.6*m - 0.2) -2*20 + 2014 + (2014//4) + (20//4))
    return day[weekday]
:


:

然后您可以将其传递到您的
列表中


:


如果您不能使用
datetime
,则可以将其传递到您的
列表中

,这样应该可以:

def day(d, m):
    day = (sum((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[:m-1]) + d + 3) % 7
#           ^                                                     ^      ^   ^  ^ ^
#           '---- adding up the days in the months                |      |   |  | |
#                   up to but not including the current month ----'      |   |  | |
#                                  plus the current day of the month ----'   |  | |
#                                  and the day of the week on 12/31/2013 ----'  | |
#                    modulus (%) is what's left over after integer division ----' |
#                                                        seven days in a week ----'

如果您不能使用
日期时间
,则应使用以下方法:

def day(d, m):
    day = (sum((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[:m-1]) + d + 3) % 7
#           ^                                                     ^      ^   ^  ^ ^
#           '---- adding up the days in the months                |      |   |  | |
#                   up to but not including the current month ----'      |   |  | |
#                                  plus the current day of the month ----'   |  | |
#                                  and the day of the week on 12/31/2013 ----'  | |
#                    modulus (%) is what's left over after integer division ----' |
#                                                        seven days in a week ----'

DateTime
立即为您修复此问题。检查一下文件就行了。如果您提供施工日期;您可以使用格式化程序输出
日期
。不要重新发明轮子;)<代码>日期时间
立即为您修复此问题。检查一下文件就行了。如果您提供施工日期;您可以使用格式化程序输出
日期
。不要重新发明轮子;)啊,我已经考虑过使用导入日期时间,但是,我还没有涵盖这一部分。还有其他方法吗?从datetime导入datetime执行
。甚至可以给它起一个新的(较短的)名字:)@Yozuru,如果你只在2014年需要它,那么就列出每个月有多少天,加上之前所有的月份,加上今天,再加上2014年1月1日的星期几。模数(%)7,你们完成了。@mhlester,所以如果我是正确的,这将使用Zeller的同余。这个列表看起来好像我不熟悉泽勒的一致性。我考虑过使用importdatetime,但是,我还没有涉及到这一部分。还有其他方法吗?从datetime导入datetime执行
。甚至可以给它起一个新的(较短的)名字:)@Yozuru,如果你只在2014年需要它,那么就列出每个月有多少天,加上之前所有的月份,加上今天,再加上2014年1月1日的星期几。模数(%)7,你们完成了。@mhlester,所以如果我是正确的,这将使用Zeller的同余。这个列表看起来好像我不熟悉泽勒的一致性。只是做数学题
def day(d, m):
    day = (sum((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[:m-1]) + d + 3) % 7
#           ^                                                     ^      ^   ^  ^ ^
#           '---- adding up the days in the months                |      |   |  | |
#                   up to but not including the current month ----'      |   |  | |
#                                  plus the current day of the month ----'   |  | |
#                                  and the day of the week on 12/31/2013 ----'  | |
#                    modulus (%) is what's left over after integer division ----' |
#                                                        seven days in a week ----'