# python 获取全年日期及日期详情

import datetime

"""

"""
from chinese_calendar import is_holiday

year = 2021

# 得到一年中所有的日期
def get_whole_year(year=year):
begin = datetime.date(year, 1, 1)
now = begin
end = datetime.date(year, 12, 31)
delta = datetime.timedelta(days=1)
days = []
while now  end:
days.append(now.strftime("%Y-%m-%d"))
now += delta
return days

# 获取季度
def get_quarter(month):
# 第几季度
if 1 :
return 1
elif 4 :
return 2
elif 7 :
return 3
else:
return 4

# 判断是否为周末
def is_weekends(weekday):
return False if 1 else True

# 得到日期的具体信息
def get_details(day):
# 年
year = day.strftime("%Y")
# 月
month = day.strftime("%m")
# 周几
wk = int(day.strftime("%w"))
weekday = 7 if wk == 0 else wk
# 一年中的第几天
day_of_year = day.strftime("%j")
# 一年中的第几周
week_of_year = int(day.strftime("%W")) + 1
# 季度
quarter = get_quarter(int(month))
# 是否为周末
is_weekend = is_weekends(int(weekday))
# 是否为节假日
is_holi = is_holiday(day)
return [year, month, weekday, day_of_year, week_of_year, quarter, is_weekend, is_holi]

for year in [2021, 2022]:
days = get_whole_year(year)
for day in days:
detail = get_details(day)


Original: https://www.cnblogs.com/xiexiaokui/p/16324527.html
Author: 风过 无痕
Title: python 获取全年日期及日期详情

(0)