时间(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小时
相关推荐
久久学姐11 分钟前
【AI问答】python引入其他模块的对象
python·绝对导入·相对导入·目录结构·模块导入
always_TT38 分钟前
【Python requirements.txt 依赖管理】
开发语言·python
段一凡-华北理工大学1 小时前
AI推动工业智能化转型~系列文章07:分类与诊断算法体系:故障识别的完整工具箱
数据库·人工智能·算法·机器学习·分类·数据挖掘·高炉炼铁智能化
Logintern091 小时前
py文件开头为什么需要加:from __future__ import annotations
开发语言·python
bamb001 小时前
一个项目带你入门AI应用开发05
python
Python私教1 小时前
Django 6.1 邮件配置大改:旧项目如何平稳升级?
后端·python·django
Python私教1 小时前
Django 6.1 升级避坑:数据库版本不兼容怎么解决?
后端·python·django
Python私教1 小时前
Django 接口开发实测:新手还需要使用 REST 框架吗?
后端·python·django
名字还没想好☜2 小时前
Python concurrent.futures 实战:用 ThreadPoolExecutor 并发处理 + as_completed 收结果
数据库·python·php·并发
爱码小白2 小时前
importlib模块
开发语言·前端·python