发布网友 发布时间:2022-05-10 09:55
共2个回答
懂视网 时间:2022-05-10 14:16
第一种方法:from datetime import datetime, timedelta weekdays = ['Monday','Tuesday','Wednesday','Thursday', 'Friday','Saturday','Sunday'] def get_previous_byday(dayname, start_date=None): if start_date is None: start_date = datetime.today() day_num = start_date.weekday() day_num_target = weekdays.index(dayname) days_ago = (7 + day_num - day_num_target) % 7 if days_ago == 0: days_ago = 7 target_date = start_date - timedelta(days = days_ago) return target_date print( datetime.today() ) print( get_previous_byday('Monday') ) print( get_previous_byday('Monday', datetime(2016, 8, 28)) )
第二种方法,用dateutil模块
from datetime import datetime from dateutil.relativedelta import relativedelta from dateutil.rrule import * d = datetime.now() print(d) print(d + relativedelta(weekday=FR)) print(d + relativedelta(weekday=FR(-1)))
热心网友 时间:2022-05-10 11:24
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]
days = 100
print(get_week_day(datetime.datetime.now() + datetime.timedelta( days )))