Python_datetime

Refer to official documentation:

datetime — Basic date and time types — Python 3.12.4 documentation

1)时区问题

Date and time objects are categorized as "aware" and "naive", as the former include time-zone information while the latter not.

Determining if an object is aware or naive:

1) date: always naive

2) datetime: if aware, datetime.tzinfo != None & datetime.tzinfo.utcoffset(datetime) != None

3) time: if aware, time.tzinfo != None & time.tzinfo.utcoffset(time) != None

2)两个时间的差值问题

Class datetime.timedelta:

        A duration expressing the difference between two datetime or date instances to microsecond resolution. distinction between aware and naive doesn't apply to timedelta objects. When create a objects of timedelta type, week, hour, minute and millisecond are optional and default to 0, but will be converted to days, seconds and microseconds, which are stored internally.

        deltatime to hours: 

                total_hours = int(delta.total_seconds() / 3600)

        compare to a definite deltatime:

                zday = datetime.timedelta(days=0, seconds=0, microseconds=0)
                aday = datetime.timedelta(days=1, seconds=0, microseconds=0)

3)字符串识别问题

字符串识别为日期:

日期转换为字符串:

    try:
        dif=img_date-ope_t
    except:
        try:
            ope_t=datetime.strptime(ope_t,'%Y-%m-%d %H:%M:%S')
            dif=img_date-ope_t
        except:
            ope_t=datetime.strptime(in_date[idx],'%Y-%m-%d %H:%M:%S')
            dif=img_date-ope_t
            
    nifti_post_ope_period.append(dif.days)

Refer to a note about timedelta transformation:

Python 如何获取 timedelta 在 Python 中的总小时数和分钟数|极客教程 (geek-docs.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值