如何计算两个 ISO 日期之间的周数

How to calculate number of weeks between two ISO dates

我正在逐行阅读 excel 文件,其中一列 ISO year_week_start : 2020_53 和第二个 ISO year_week_end : 2021_01

我正在寻找如何获得这两个字符串之间的距离

2021_01 - 2020_53 => 1
2020_40 - 2020_30 => 10

这是您要找的吗?

from datetime import date

def convert(value):
    (year, week) = [int(x) for x in value.split('_')]
    result = date.fromisocalendar(year, week, 1)
    return result

def distance(date1, date2):
    date1 = convert(date1)
    date2 = convert(date2)
    diff = date1 - date2
    weeks = diff.days / 7
    return weeks

print(distance('2021_01', '2020_53'))
print(distance('2020_40', '2020_30'))
print(distance('2021_01', '1991_01'))

输出

1.0
10.0
1566.0