python判断某个日期在所属年份的第几周

import datetime
 
def convert_date(date_string):
    format_form = ['%Y-%m-%d', '%Y-%m-%d %H:%M:%S', '%Y/%m/%d', '%Y/%m/%d %H:%M:%S']
    for form in format_form:
        try:
            formated_date = datetime.datetime.strptime(date_string, form)
            break
        except Exception:
            continue
    else:
        print(f'当前外观总检时间({date_string})格式不支持读取,请更换日期格式!')
        return ''
    year = formated_date.year
    if int(datetime.datetime(year, 1, 1).strftime('%W')) != 1:
        current_week = int(formated_date.strftime('%W')) + 1
    else:
        current_week = int(formated_date.strftime('%W'))
    if current_week < 10:
        format_week = f'W0{current_week}'
    else:
        format_week = f'W{current_week}'
    return format_week
 
convert_date('2023-01-01') # W01

你可能感兴趣的:(python,开发语言)