2022年 11月 7日

python获取一整年的年月日

import arrow


def getAllYearDate(year: int):
    """
    :param year: 年份,eg:2022、2023等
    :return:
    """
    date_list = []
    start_date = '%s-1-1' % year
    a = 0
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        days_sum = 366
    else:
        days_sum = 365
    while a < days_sum:
        b = arrow.get(start_date).shift(days=a).format("YYYYMMDD")
        a += 1
        date_list.append(b)
    return date_list

print(getAllYearDate(2023))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22