此计划在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]
答案 0 :(得分:7)
>>> import datetime
>>> datetime.datetime(2014, 2, 16).strftime('%a')
'Sun'
>>> import datetime
>>> datetime.datetime(2014, 2, 16).weekday()
6
然后您可以将其传递到day
列表
答案 1 :(得分:1)
如果你不能使用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 ----'