python - compare the date in the string with today's date

  • Last Update :
  • Techknowledgy :

You need to parse the time part as well:

datetime.strptime(dateTimeStart, "%Y-%m-%d %H:%M:%S")

Then you can compare your dates like this:

if dateTime1.date() > date.today():
   print "No"

Suggestion : 2

Last Updated : 24 May, 2018

Output :

d1 is greater than d2: False
d1 is less than d2: True
d1 is not equal to d2: True

Suggestion : 3

By the way, date.today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.,Here, we imported the date class from the datetime module. Then, we used the date.today() method to get the current local date.,In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime() method.,There are a number of ways you can take to get the current date. We will use the date class of the datetime module to accomplish this task.

Example 1: Python get today's date

from datetime
import date

today = date.today()
print("Today's date:", today)

Example 2: Current date in different formats

from datetime
import date

today = date.today()

# dd / mm / YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm / dd / y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)

When you run the program, the output will be something like:

d1 = 16 / 09 / 2019
d2 = September 16, 2019
d3 = 09 / 16 / 19
d4 = Sep - 16 - 2019

You will gate output like below.

now = 2021 - 06 - 25 07: 58: 56.550604
date and time = 25 / 06 / 2021 07: 58: 56

Suggestion : 4

You can use less than comparison operator < to check if one datetime object is less than other.,In the following program, we initialize two datetime objects, and then check if both datetime objects have same date and time.,In the following program, we initialize two datetime objects, and then compare if first one is less than second.,In the following program, we initialize two datetime objects, and then compare if first one is greater than second.

Python Program

import datetime

# date and time in yyyy / mm / dd hh: mm: ss format
d1 = datetime.datetime(2020, 5, 13, 22, 50, 55)
d2 = datetime.datetime(2020, 7, 13, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 13, 22, 50, 55)

print(d1 > d2)
print(d2 > d3)

Output

False
True

Suggestion : 5

We will use less than operator < to check if one datetime object is greater than other datetime objects.,We will use greater than operator > to check if one datetime object is greater than other datetime objects.,We will use equal to comparison operator = to check if one datetime object has the same value as the other.,Using datetime module we can determine which date is an earlier date, which date is the latest, or which two dates are equal depending upon the dates available. We compare dates on the basis of date format as well as on the basis of time format. Now, to compare datetime objects, we can use comparison operators like greater than, less than, or equal to. We know that in Python comparison operators return boolean values (True or False). Similarly, this function will return either True or False. If the condition gets true then it will print True else False.

In the given example, we have initialized three datetime objects in the format of yyyy/ mm/ dd hh : mm: ss, and then compared if the first date is greater than another date.

import datetime
# date and time in yyyy / mm / dd hh: mm: ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55)
d2 = datetime.datetime(2020, 7, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 > d2)
print(d2 > d3)

In the given example, we have initialized three datetime objects in the format of yyyy/ mm/ dd hh : mm: ss, and then compared if the first date is less than another date.

import datetime
# date and time in yyyy / mm / dd hh: mm: ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55)
d2 = datetime.datetime(2020, 7, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 < d2)
print(d2 < d3)

In the following program, we initialize three datetime objects and then check if both datetime objects have the same date or not.

import datetime
# date and time in yyyy / mm / dd hh: mm: ss format
d1 = datetime.datetime(2020, 5, 11, 22, 50, 55)
d2 = datetime.datetime(2020, 5, 11, 22, 50, 55)
d3 = datetime.datetime(2020, 6, 11, 22, 50, 55)
print(d1 == d2)
print(d2 == d3)