'Get the highest of two string variables representing dates

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Srinivas Reddy Thatiparthy