如何更改日期时间分辨率?

有没有简单的方法可以说明

a = '2020-01-01 19:30:33.996628' 
b = '2020-01-01 19:30:34' 

a and b are equal. If the time resolution of a is changed to second, then a could be equal to b. How to implement it with code?

评论
T和T
T和T

if you treat both of them as datetime objects, you can use arithmetic operators on them. For example, you can subtract them and check if the result satisfy a condition (like less then one minute different) as you wise

点赞
评论
撸啊撸
撸啊撸
  • Use .replace(microsecond=0)
from datetime import datetime

a = datetime.fromisoformat('2020-01-01 19:30:33.996628')

print(a)

>>> datetime.datetime(2020, 1, 1, 19, 30, 33, 996628)

a = a.replace(microsecond=0)

print(a)

>>> datetime.datetime(2020, 1, 1, 19, 30, 33)
点赞
评论