转换日期时间以允许减法
Convert datetimes to allow subtraction
我认为这是一个简单的问题,但我却非常挣扎......
我需要减去两个日期时间。一个是幼稚的,一个是有意识的,但我终生无法将一个转换为另一个。
如何将这些日期时间之一转换为另一个以允许减法?
示例代码如下:
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)
returns
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
所以,你要么需要让第一次约会有意识,要么让第二次约会天真。
current_time = datetime.datetime.now().astimezone()
或
x = datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f")
我认为这是一个简单的问题,但我却非常挣扎...... 我需要减去两个日期时间。一个是幼稚的,一个是有意识的,但我终生无法将一个转换为另一个。
如何将这些日期时间之一转换为另一个以允许减法?
示例代码如下:
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)
returns
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
所以,你要么需要让第一次约会有意识,要么让第二次约会天真。
current_time = datetime.datetime.now().astimezone()
或
x = datetime.strptime(x, "%Y-%m-%d %H:%M:%S.%f")