Python怎样实现周日历,转换为日期怎样做
Admin 2022-07-26 群英技术资讯 1320 次浏览
在这篇文章中,我们来学习一下“Python怎样实现周日历,转换为日期怎样做”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。周日历(ISO国际标准)
介绍在线周日历(2022年)
在开发过程中,有些汇总咨询需要以周为单位统计,所以介绍下如何进行相互转换。
strftime 方法可以将时间转换为字符串
strptime 方法可以将字符串转为时间
"%Y,%W,%w"中,"%Y"代表年份,"%W"代表周,"%w"代表一周内的第几天
from datetime import datetime
# 时间转周日历
a = datetime.now().strftime("%Y,%W,%w")
print(a) # 2022,28,3
# 周日历转时间
a = datetime.strptime("2022,12,3","%Y,%W,%w")
print(a) # 2022-03-23 00:00:00
以上貌似问题解决了,但是问题出在年初和年尾
以2021年12月,2022年1月举例
2021年12月
| 周数 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 | 周日 |
|---|---|---|---|---|---|---|---|
| 48 | 1 | 2 | 3 | 4 | 5 | ||
| 49 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 50 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 51 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 52 | 27 | 28 | 29 | 30 | 31 |
2022年1月
| 周数 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 | 周日 |
|---|---|---|---|---|---|---|---|
| 52 | 1 | 2 | |||||
| 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 3 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 4 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 5 | 31 |
from datetime import datetime
a = datetime.strptime("2021-12-31", "%Y-%m-%d")
print(a.strftime("%Y,%W,%w")) # 2021,52,5
a = datetime.strptime("2022-01-01", "%Y-%m-%d")
print(a.strftime("%Y,%W,%w")) # 2022,00,6
按iso标准,2022年1月1日应该归为2021年的最后一周
使用strftime方法格式化后为2022年第0月,所以这是有问题的
datetime类型的时间直接调用 isocalendar 方法
from datetime import datetime
def str_to_time(time_str: str) -> datetime:
return datetime.strptime(time_str, "%Y-%m-%d")
time_list = [
"2021-12-30",
"2021-12-31",
"2022-01-01",
"2022-01-02",
"2022-01-03",
]
for i in time_list:
t = str_to_time(i)
iso = t.isocalendar()
print(i, " > ", f"{iso.year},{iso.week},{iso.weekday}")
# 2021-12-30 > 2021,52,4
# 2021-12-31 > 2021,52,5
# 2022-01-01 > 2021,52,6
# 2022-01-02 > 2021,52,7
# 2022-01-03 > 2022,1,1
from datetime import datetime
time_list = (
(2021, 52, 4),
(2021, 52, 5),
(2021, 52, 6),
(2021, 52, 7),
(2022, 1, 1),
)
for year, week, weekday in time_list:
t = datetime.fromisocalendar(year, week, weekday)
print(f"{year},{week},{weekday}", " > ", t)
# 2021,52,4 > 2021-12-30 00:00:00
# 2021,52,5 > 2021-12-31 00:00:00
# 2021,52,6 > 2022-01-01 00:00:00
# 2021,52,7 > 2022-01-02 00:00:00
# 2022,1,1 > 2022-01-03 00:00:00
from datetime import datetime
def datetime_to_isoweek(datetime_: datetime) -> tuple[int, int, int]:
"""时间转换为iso周日历
Args:
datetime_ (datetime): 时间
Returns:
tuple[int,int,int]: year,week,weekday
"""
iso = datetime_.isocalendar()
return iso.year, iso.week, iso.weekday
def isoweek_to_datetime(isoweek: tuple[int, int, int]) -> datetime:
"""iso周日历转换为时间
Args:
isoweek (tuple[int,int,int]): year,week,weekday
Returns:
datetime: 时间
"""
year, week, weekday = isoweek
return datetime.fromisocalendar(year, week, weekday)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python实战之利用pygame实现贪吃蛇游戏,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好的帮助哟,需要的朋友可以参考下
PIL是Python Imaging Library的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移,旋转,改变尺寸等等。已
数字数据类型用于存储数值。它们是不可变的数据类型,这意味着需要改变一个新分配对象的数字数据类型的结果值。
Python进行图片处理,第一步就是读取图片,下面这篇文章主要给大家介绍了关于基于python读取图像的几种方式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。虽然使用递归可以快速的解决一些难题,但由于递归的抽象性,使递归难以掌握。为了更好的理解递归函数背后的思想,本节主要通过可视化方式来了解递归函数的执行步骤。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008