Home > Net >  How to change the format of the dates and compare it
How to change the format of the dates and compare it

Time:07-18

I have got two variable as

x = 27.01.2022 
y= 12.06.2022

I want to compare two dates and want to print true if y > x else print false. I am doing this:

import datetime

x = datetime.datetime(27.01.2022)
y = datetime.datetime(12.06.2022)

if x>y:
    print('True')
else:
    print('False')
File "<string>", line 3
    x = datetime.datetime(27.01.2022)
                               ^
SyntaxError: invalid syntax

How to resolve this?

CodePudding user response:

Neither of your code blocks are valid syntax. Please refer documentation on examples of using datetime instances

You don't have a time, so you really only need date objects. And you can print a boolean result directly

from datetime import date
x = date.fromisoformat('2022-01-27')
y = date.fromisoformat('2022-06-12')
print(x > y)
  • Related