时间(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小时
相关推荐
2601_962295335 分钟前
如何python实现网页的自动化
python·selenium·beautifulsoup·requests·网页自动化
ofoxcoding16 分钟前
GPT Image 2.5 API 实战:Python 调用实现图片生成与编辑
人工智能·python·gpt·ai
张小姐的猫1 小时前
【AI大模型接入SDK】 —— Gemini接入封装
android·数据结构·数据库·c++·人工智能·python
Elastic 中国社区官方博客1 小时前
Elasticsearch Python DSL 客户端开发
大数据·数据库·python·elasticsearch·搜索引擎·全文检索
2601_962780692 小时前
2026 秋招数据产品分析岗位技能拆解
数据分析
计算机编程-吉哥2 小时前
脑肿瘤MRI智能识别系统:基于深度学习的像素级脑肿瘤语义分割平台【计算机毕业设计选题推荐】
人工智能·python·深度学习·算法·毕业设计·课程设计·大数据毕业设计选题推荐
小木_.2 小时前
Python 离线识别滑块缺口距离,项目推荐
开发语言·python·滑块识别·人机验证·滑块缺口·缺口识别
Cenxi2 小时前
Python字符串方法练习手册
人工智能·python
liliangcsdn2 小时前
因子权重矩阵处理-因子权重收缩Shrinkage算法的探索
开发语言·python·机器学习
用户8356290780512 小时前
使用 Python 在 Excel 中添加和编辑形状
后端·python