python - Python中的日期时间
问题描述
我有两个约会
'2017-03-10 19:01:27'
'2017-03-11 07:35:55'
我想有一天得到差异。如何在考虑时区的情况下做到这一点?
因为我们不知道这些日期的时区。我是 Python 新手
我有:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
date_first = datetime.strptime(args[0], time_format)
date_second = datetime.strptime(args[1], time_format)
rep_date_first = date_first.replace(tzinfo=from_zone)
rep_date_second = date_second.replace(tzinfo=from_zone)
timezone_date_first = rep_date_first.astimezone(to_zone)
timezone_date_second = rep_date_second.astimezone(to_zone)
day = timezone_date_first.day - timezone_date_second.day
day return me 0,如何解决?请帮我
解决方案
arrow
模块是每个处理日期的人的好朋友,你很容易实现转变。
import arrow
dt = arrow.get('2017-03-10 19:01:27')
dt = dt.replace(tzinfo='Europe/Warsaw')
dt2 = dt.shift(days=-1)
assert dt2.isoformat() == '2017-03-09T19:01:27+01:00'
你问的有点友好:I would like to get the difference one day
你的代码不能单独运行(args[0]
),但我认为你可以假设日期在同一个时区。
推荐阅读
- node.js - Cpanel 上的 Nodejs 环境变量
- java - 为什么java队列在某些情况下无法轮询数据?
- python - 为什么我在 pygame 上的圈子不断消失和重新生成?
- git - 将两台不同机器上的两个预先存在的本地文件夹合并到一个共享的在线存储库中
- unreal-engine4 - 在虚幻引擎 4 中构建光照后的黑色树叶
- c++ - 将人员添加到结构的 STL 列表
- javascript - 使用用户定义类型数组作为参数在nodejs中调用DB2存储过程
- c - 为什么在 C 中使用浮点转换说明符时没有输出
- ios - SwiftUI:语音识别通过 ipad 上的崩溃在 iphone 上工作
- jquery-easyui - 如何禁用jquery-easyui的标签滚动