Python实现周日历与时间相互转换,


目录
  • 前言
  • 基本介绍
    • 使用datetime类格式化进行转换
    • 问题
  • 正确方法
    • 使用isocalendar将日期转换为周日历
    • 使用 fromisocalendar 将周日历转换为日期
  • python代码

    前言

    周日历(ISO国际标准)

    介绍在线周日历(2022年)

    基本介绍

    在开发过程中,有些汇总咨询需要以周为单位统计,所以介绍下如何进行相互转换。

    使用datetime类格式化进行转换

    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  12345
    496789101112
    5013141516171819
    5120212223242526
    522728293031  

    2022年1月

    周数周一周二周三周四周五周六周日
    52     12
    13456789
    210111213141516
    317181920212223
    424252627282930
    531      
    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月,所以这是有问题的

    正确方法

    使用isocalendar将日期转换为周日历

    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

    使用 fromisocalendar 将周日历转换为日期

    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]:
        """时间转换为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)

    以上就是Python实现周日历与时间相互转换的详细内容,更多关于Python周日历与时间互换的资料请关注3672js教程其它相关文章!

    您可能感兴趣的文章:
    • 利用JavaScript实现ISO周日历
    • vue实现周日历切换效果
    • 微信小程序可滑动周日历组件使用详解
    • python时间与Unix时间戳相互转换方法详解
    • python正常时间和unix时间戳相互转换的方法

    评论关闭