题目
给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。
输入为三个整数:day、month 和 year,分别表示日、月、年。
您返回的结果必须是这几个值中的一个 {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}。
示例
输入:day = 31, month = 8, year = 2019
输出:“Saturday”
代码
版本1
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
from datetime import datetime
res={2:"Sunday", 3:"Monday", 4:"Tuesday", 5:"Wednesday", 6:"Thursday", 0:"Friday", 1:"Saturday"}
start = datetime(1971, 1, 1)
days=(datetime(year, month, day)- start).days
return res[days%7]
版本2
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
from datetime import datetime
x = datetime(year, month, day)
return x.strftime("%A")