Python如何根据日期判断周几
Python判断周几主要使用了time, datetime库,具体实现方法如下。
方法一:
#!/usr/bin/python # -*- coding:utf-8 -*- from datetime import datetime today = datetime.now().weekday() + 1 print(today) week = datetime.strptime("20191018","%Y%m%d").weekday() + 1 print(week)
方法二:
#!/usr/bin/python # -*- coding:utf-8 -*- from datetime import datetime apply_time = 20191018 apply_time = datetime.utcfromtimestamp(apply_time) time_str = apply_time.strftime("%Y-%m-%d %H:%M:%S") apply_hour = time_str.split(" ")[1].split(":")[0] apply_week = datetime.strptime(time_str.split(" ")[0], "%Y-%m-%d").weekday() print(apply_week)
方法三:
#!/usr/bin/python # -*- coding:utf-8 -*- import time, datetime def get_week_day(date): week_day_dict = { 0: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五', 5: '星期六', 6: '星期天', } day = date.weekday() return week_day_dict[day] print(get_week_day(datetime.datetime.now()))
更多技术请关注Python视频教程。