I have two variables (a
and b
) in the DateTime format 'YYYYMMDDTHHMMSS' (e.g. '20180108T124506'), and I would like to know how to decide which one is more recent. So essentially the highest number.
The problem is that I can't convert it to an integer using int(variable)
because there's a T in the variable.
How can I circumvent this problem?
CodePudding user response:
There is no need to do anything. YYYYMMDDTHHMMSS
format is already ordered from most to least significant; this means the native str
order is equivalent to the desired "time" order.
>>> a = "20180108T124506"
>>> b = "20220108T124506"
>>> b > a
True
>>> max(a, b)
'20220108T124506'
CodePudding user response:
You can use this,
from datetime import datetime
a = datetime.strptime("20180108T124506","%Y%m%dT%H%M%S")
b = datetime.strptime("20220108T124506","%Y%m%dT%H%M%S")
In [11]: b > a
Out[11]: True