时间(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小时
相关推荐
Starry-sky(jing)11 分钟前
RecursionError: maximum recursion depth exceeded —— 你的函数调用链,踩穿了 CPython 的安全气囊
python·cpython·pydantic·recursionerror·递归深度·递归限制·sys.setrecursionlimit
cui_ruicheng15 分钟前
Python从入门到实战(三):流程控制与循环语句
开发语言·python
Rauser Mack25 分钟前
Vibe coding游戏实战:零代码编程五子棋小游戏
人工智能·python·游戏·html·prompt
FriendshipT39 分钟前
Ultralytics:解读PSABlock模块
人工智能·pytorch·python·深度学习·目标检测
hhzz1 小时前
全局实例跟踪(GIT):像人类一样定位目标——VideoCube基准与SiamFC实战全解析
大数据·python·计算机视觉·目标跟踪·数据分析
hhzz10 小时前
基于监控视频的水位尺自动识别技术方案与实现
python·opencv·yolo·图像识别·cv
yongche_shi10 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
weixin_4080996711 小时前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)
图像处理·python·ocr·文字识别·api调用·批量识别·石榴智能
AI行业学习11 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程12 小时前
Python中continue语句的用法是什么?
开发语言·前端·python