Python Compare Two Dates Example Tutorial

Published On: 04/10/2022 | Category: Python

Hi Guys,

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 Today
from datetime import datetime
myDateString = "2022-10-05"
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
# Today's Date is 2022-10-04
todayDate =
if >
    print("Date is Greater than Today")
    print("Date is not Greater than Today")
Date is Greater than Today

Example 2: Python Compare with Two Dates
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 >
    print("First Date is Greater...")
    print("Second Date is Greater...")

First Date is Greater...

It will help you....

Happy Pythonic Coding!