我是靠谱客的博主 粗犷睫毛,这篇文章主要介绍【趣味编程】根据年份和周数获取第n周周一对应的日期(经济政治上的周一)【趣味编程】根据年份和周数获取第n周周一对应的日期(经济政治上的周一),现在分享给大家,希望可以做个参考。
【趣味编程】根据年份和周数获取第n周周一对应的日期(经济政治上的周一)
#!/usr/bin/env python
# -*- coding = utf-8 -*-
# @Time : 2021/9/30 20:55
# @Author : 陈良兴
# @File : test_week
# @Software : PyCharm
import datetime
from calendar import *
import pyautogui
def date_conversation(year, week):
'''
已知年份和周数,获取第N周的周一对应的日期
:param year: 年份
:param week: 周数
:return:第n周周一对应的日期
'''
# 输入的字符串类型的年和日转换为整型
day = int((int(week) - 1) * 7 + 1)
year = int(year)
# first_day:此年的第一天
first_day = datetime.datetime(year, 1, 1) # 2021-01-01 00:00:00
# 判断每年的第一周周一
first_day_text = str(first_day)
lis = ['1', '2', '3', '4', '5', '6', '7']
y = first_day_text[0:4]
m = first_day_text[5:7]
d = first_day_text[8:10]
dic = dict(enumerate(lis))
# 每年第一天是周几
if y.isdigit() and m.isdigit() and d.isdigit() and 1 <= int(m) <= 12 and 1 <= int(d) <= 31:
w = dic[weekday(int(y), int(m), int(d))]
if int(w) != 1:
target_day = first_day + datetime.timedelta(day + 7 - int(w))
else:
target_day = first_day + datetime.timedelta(day - int(w))
# 返回需要的字符串形式的日期
target_day = datetime.datetime.strftime(target_day, '%Y年%m月%d日')
return target_day
if __name__ == '__main__':
while True:
year = input("年份:")
week = input("第几周:")
if int(week) <= 53:
text = "{}年的第{}周周一对应的日期为:".format(year, week) + date_conversation(year, week)
print(text)
print("---------------------------------------------------------------------------------------------")
bottons = ['OK', 'Cancel']
b = pyautogui.confirm(text=text, title='result', buttons=bottons)
if len(b) == 2:
continue
else:
break
else:
bottons = ['OK', 'Cancel']
a = pyautogui.confirm(text=r'周数错误,一年周数为53,请输入小于或等于53的数!!!', title='警告', buttons=bottons)
if len(a) == 2:
continue
else:
break
输出结果:
最后
以上就是粗犷睫毛最近收集整理的关于【趣味编程】根据年份和周数获取第n周周一对应的日期(经济政治上的周一)【趣味编程】根据年份和周数获取第n周周一对应的日期(经济政治上的周一)的全部内容,更多相关【趣味编程】根据年份和周数获取第n周周一对应内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复