以下是一个将日期转换为当年第几周的函数:。
```Python。
import datetime。
def date_to_week(date):。
year = date.year。
first_day_year = datetime.date(year, 1, 1)。
days = (date - first_day_year).days。
week = (days // 7) + 1。
return week。
```。
使用示例:。
```Python。
date = datetime.date(2021, 10, 15)。
week = date_to_week(date)。
print(f"The date {date} is in the {week}th week of the year.")。
```。
输出:。
```。
The date 2021-10-15 is in the 41th week of the year.。