python獲取一年全部的日期

python獲取一年全部的日期python

自動識別閏年。app

import arrow


def isLeapYear(years):
    '''
    經過判斷閏年,獲取年份years下一年的總天數
    :param years: 年份,int
    :return:days_sum,一年的總天數
    '''
    # 斷言:年份不爲整數時,拋出異常。
    assert isinstance(years, int), "請輸入整數年,如 2018"

    if ((years % 4 == 0 and years % 100 != 0) or (years % 400 == 0)):  # 判斷是不是閏年
        # print(years, "是閏年")
        days_sum = 366
        return days_sum
    else:
        # print(years, '不是閏年')
        days_sum = 365
        return days_sum


def getAllDayPerYear(years):
    '''
    獲取一年的全部日期
    :param years:年份
    :return:所有日期列表
    '''
    start_date = '%s-1-1' % years
    a = 0
    all_date_list = []
    days_sum = isLeapYear(int(years))
    print()
    while a < days_sum:
        b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
        a += 1
        all_date_list.append(b)
    # print(all_date_list)
    return all_date_list


if __name__ == '__main__':
    # years = "2001"
    # years = int(years)
    # # 經過判斷閏年,獲取一年的總天數
    # days_sum = isLeapYear(years)

    # 獲取一年的全部日期
    all_date_list = getAllDayPerYear("2000")
    print(all_date_list)

  

輸出:orm

相關文章
相關標籤/搜索