datetime.date
has a isocalendar()
method, which returns a tuple containing the calendar week:
>>> import datetime
>>> datetime.date(2010, 6, 16).isocalendar()[1]
24
datetime.date.isocalendar() is an instance-method returning a tuple containing year, weeknumber and weekday in respective order for the given date instance.
In Python 3.9+ isocalendar()
returns a namedtuple with the fields year
, week
and weekday
which means you can access the week explicitly using a named attribute:
>>> import datetime
>>> datetime.date(2010, 6, 16).isocalendar().week
24
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…