網(wǎng)站首頁(yè) 編程語言 正文
前言
周日歷(ISO國(guó)際標(biāo)準(zhǔn))
介紹在線周日歷(2022年)
基本介紹
在開發(fā)過程中,有些匯總咨詢需要以周為單位統(tǒng)計(jì),所以介紹下如何進(jìn)行相互轉(zhuǎn)換。
使用datetime類格式化進(jìn)行轉(zhuǎn)換
strftime 方法可以將時(shí)間轉(zhuǎn)換為字符串
strptime 方法可以將字符串轉(zhuǎn)為時(shí)間
"%Y,%W,%w"中,"%Y"代表年份,"%W"代表周,"%w"代表一周內(nèi)的第幾天
from datetime import datetime
# 時(shí)間轉(zhuǎn)周日歷
a = datetime.now().strftime("%Y,%W,%w")
print(a) # 2022,28,3
# 周日歷轉(zhuǎn)時(shí)間
a = datetime.strptime("2022,12,3","%Y,%W,%w")
print(a) # 2022-03-23 00:00:00
問題
以上貌似問題解決了,但是問題出在年初和年尾
以2021年12月,2022年1月舉例
2021年12月
周數(shù) | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 | 周日 |
---|---|---|---|---|---|---|---|
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月
周數(shù) | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 | 周日 |
---|---|---|---|---|---|---|---|
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標(biāo)準(zhǔn),2022年1月1日應(yīng)該歸為2021年的最后一周
使用strftime方法格式化后為2022年第0月,所以這是有問題的
正確方法
使用isocalendar將日期轉(zhuǎn)換為周日歷
datetime類型的時(shí)間直接調(diào)用 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
使用 fromisocalendar 將周日歷轉(zhuǎn)換為日期
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
python代碼
from datetime import datetime
def datetime_to_isoweek(datetime_: datetime) -> tuple[int, int, int]:
"""時(shí)間轉(zhuǎn)換為iso周日歷
Args:
datetime_ (datetime): 時(shí)間
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周日歷轉(zhuǎn)換為時(shí)間
Args:
isoweek (tuple[int,int,int]): year,week,weekday
Returns:
datetime: 時(shí)間
"""
year, week, weekday = isoweek
return datetime.fromisocalendar(year, week, weekday)
原文鏈接:https://segmentfault.com/a/1190000042107154
相關(guān)推薦
- 2022-11-07 React?全面解析excel文件_React
- 2022-09-01 寫一個(gè)shell腳本實(shí)現(xiàn)視頻處理_linux shell
- 2022-04-19 asp.core?同時(shí)兼容JWT身份驗(yàn)證和Cookies?身份驗(yàn)證兩種模式(示例詳解)_實(shí)用技巧
- 2023-01-07 Python中sys.argv用法圖文詳解_python
- 2023-07-08 前端加載報(bào)錯(cuò)Cannot assign to read only property ‘exports
- 2022-08-06 python練習(xí)之循環(huán)控制語句?break?與?continue_python
- 2022-08-27 JQuery自定義模態(tài)框效果_jquery
- 2022-12-05 Android?RecyclerView緩存復(fù)用原理解析_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支