转换日期时间以允许相减

问题描述 投票:0回答:1

我认为这是一个简单的问题,但我正在努力奋斗...我需要减去两个日期时间。一个人天真,一个人知道,但是我不能为自己的一生而将另一个人转换为另一个人。

如何将这些日期时间之一转换为另一个日期时间以允许相减?

下面的示例代码:

now_time = str(pd.Timestamp("now"))
current_time = datetime.strptime(now_time, "%Y-%m-%d %H:%M:%S.%f")
x = df.at[0,1]
x = datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f%z")
print(current_time)
print(x)
print(current_time-x)

返回

2020-04-06 19:29:58.239641
2020-04-06 22:50:22.577561+00:00
Traceback (most recent call last):
  File "main.py", line 416, in <module>
    print(current_time-x)
TypeError: can't subtract offset-naive and offset-aware datetimes

python datetime utc
1个回答
0
投票

所以,您需要知道第一个日期或天真的第二个日期。

current_time = datetime.datetime.now().astimezone()

x = datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f")
© www.soinside.com 2019 - 2024. All rights reserved.