时间(time)

python 复制代码
import datetime as dt

字符串转时间

python 复制代码
dt.datetime.strptime('20210901', '%Y%m%d')
dt.datetime.strptime('2021-09-01', '%Y-%m-%d')

 # 以上两种方式输出相同:
2021-09-01 00:00:00

计算时间差

python 复制代码
dt_2 = dt.datetime.strptime('2021-09-03 18:00', '%Y-%m-%d %H:%M') # dt_2:  2021-09-03 18:00:00
dt_1 = dt.datetime.strptime('2021-09-01 12:00', '%Y-%m-%d %H:%M') # dt_1:  2021-09-01 12:00:00

dt_delta = dt_2-dt_1 # dt_delta:  2 days, 6:00:00

d = dt_delta.days+dt_delta.seconds/(3600*24) # 相差2.25天
h = dt_delta.days*24+dt_delta.seconds/3600 # 相差54小时
相关推荐
李妍.8 分钟前
PyTorch GPU 版安装记录(RTX 5060 + Python 3.14)
人工智能·pytorch·python
90后的晨仔11 分钟前
Python 基础语法完整汇总
python
广慈新医知13 分钟前
当健康问题越来越复杂,AI更适合先帮我们“把问题问清楚”
人工智能·python
一晌小贪欢14 分钟前
python-第27天:Python面向对象详解
开发语言·python·数据可视化·面向对象·python办公
青 春 记 忆17 分钟前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
Mr数据杨2 小时前
汽车故障时间与类别预测实战 从 Kaggle 结构化赛题理解预测性维护建模
人工智能·数据分析·kaggle竞赛
智购科技自动售卖机厂家2 小时前
2026自动售货机库存热力图分析:从设备分布到补货优先级的数据可视化实践~YH
大数据·python·信息可视化
147API2 小时前
蒸馏项目什么时候该停,怎样切换到RAG、微调或模型路由
人工智能·深度学习·机器学习·数据挖掘
鹿鹿学长2 小时前
微软把语音转写打到 0.1 美元/小时:5 个月降价 72%,AI 音频进入地板价时代
python·自动化
weixin_440730502 小时前
使用pytest中方法控制执行步骤(test_begin.py、test_end.py,@pytest.mark.run(order=1))
开发语言·python·pytest