Python提供了六种方法来获取指定日期是星期几:
from datetime import date # 获取指定日期是星期几 d = date(2020, 7, 19) weekday = d.weekday() print(weekday)
import calendar # 获取指定日期是星期几 weekday = calendar.weekday(2020, 7, 19) print(weekday)
import time # 获取指定日期是星期几 weekday = time.strftime('%w', (2020, 7, 19, 0, 0, 0, 0, 0, 0)) print(weekday)
from datetime import datetime # 获取指定日期是星期几 weekday = datetime.strptime('2020-07-19', '%Y-%m-%d').weekday() print(weekday)
from dateutil.parser import parse # 获取指定日期是星期几 weekday = parse('2020-07-19').weekday() print(weekday)
import arrow # 获取指定日期是星期几 weekday = arrow.get('2020-07-19').weekday() print(weekday)
以上六种方法都可以用于获取指定日期是星期几,其中datetime模块中的date类和calendar模块的weekday()函数属于Python内置模块,其他四种方法需要安装第三方库才能使用。
本文链接:http://task.lmcjl.com/news/1830.html