一、简介
1、使用datetime获取到当天的日期
2、使用calendar判断当天的日期值
二、代码如下:
import datetime
import calendar
#获取当天日期值
currentdate = datetime.date.today()
print(currentdate)
year= currentdate.year
month = currentdate.month
day = currentdate.day
#系统默认:星期一作为一周的第一天(即:0),星期日作为一周的最后一天(即:6)
print(calendar.weekday(year,month,day))
currentday =calendar.weekday(year,month,day)
#判断当天是周几?
if currentday == 0:
print(“当天为周一”)
elif currentday ==1:
print(“当天为周二”)
elif currentday ==2:
print(“当天为周三”)
elif currentday ==3:
print(“当天为周四”)
elif currentday == 4:
print(“当天为周五”)
elif currentday == 5:
print(“当天为周六”)
else:
print(“当天为周日”)
#判断当天是否为周末
if currentday > 5:
print(“当天为周末”)
else:
print(“当天为工作日”)