Python Compare Two Dates Example Tutorial
If you need to see example of python check if date is greater than. you can understand a concept of compare two dates in python. I would like to share with you check date between two dates in python. let’s discuss about compare two dates in python. Here, Creating a basic example of python compare date with current date.
I'll provide two examples in this instance. The first example compares to the current date, while the second compares to two custom dates. Let's look at both scenarios and put them to work for you.
You can use these examples with python3 (Python 3) version.
So let's see bellow example:
Example 1: Python Check if Date is Greater than Todaymain.py
from datetime import datetime myDateString = "2022-10-05" myDate = datetime.strptime(myDateString, "%Y-%m-%d") # Today's Date is 2022-10-04 todayDate = datetime.now() if myDate.date() > todayDate.date(): print("Date is Greater than Today") else: print("Date is not Greater than Today")Output:
Date is Greater than Today
Example 2: Python Compare with Two Datesmain.py
from datetime import datetime oneDate = "2022-10-03" oneDateObj = datetime.strptime(oneDate, "%Y-%m-%d") secondDate = "2022-10-02" secondDateObj = datetime.strptime(secondDate, "%Y-%m-%d") if oneDateObj.date() > secondDateObj.date(): print("First Date is Greater...") else: print("Second Date is Greater...")Output:
First Date is Greater...
It will help you....
Happy Pythonic Coding!