Heim > Artikel > Backend-Entwicklung > Python – Berechnen Sie das Datum der letzten Woche 5
第一种方法:
Python代码
from datetime Thursday',
'Friday','Saturday','Sunday']
def get_ previous_byday(dayname, start_date=None):
wenn start_date None ist:
start_date = datetime.today ()
Day_num = start_date.weekday ()
Day_num_target = wochentage - day_num_target) % 7
if days_ago == 0:
days_ago = 7
target_date = start_date - timedelta(days = days_ago)
Rückgabezieldatum
print( datetime.today() )
print( get_ previous_byday('Monday') )
print( get_ previous_byday('Monday', datetime (2016, 8, 28)) )
第二种方法,用dateutil模块
Python代码
from datetime import datetime
from dateutil. relativedelta import relativedelta
from dateutil.rrule import *
d = datetime.now()
print(d)
print(d + relativedelta( Wochentag=FR))
print(d + relativedelta(Wochentag=FR(-1)))