第一個方法:
Python代碼
from datetime import datetime, timedelta
weekdays = [ '星期五','星期六', '星期日']
def get_previous_byday(dayname, start_date=None):
如果start_date 為None:
day_num = start_date.weekday()
day_num_target = weekdays.index(dayname )
days_ago = (7 + day_num - day_num_target) % 7
ago = 7 target_date = start_date - timedelta(day s = days_ago)datetime. Today() )
print( get_previous_byday('Monday') )
print( get_previous_byday('Monday', datetime(2016, get), il模組
Python程式碼
from datetime 導入datetime
from dateutil.relativedelta 導入relativedelta
from dateutil.rrule=print* print(d + +relativedelta(weekday=FR ))
print(d +relativedelta(工作日=FR(-1)))