日期转换当年第几周函数

以下是一个将日期转换为当年第几周的函数:。

```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.。

您可能还会喜欢:
本周是今年第几周函数
出生日期算年龄的函数
出生日期转换生辰八字对照表万年历