JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Comparing two dates in Python

Author:JIYIK Last Updated:2025/05/06 Views:

This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods.


Comparing two dates in Python using datetimethe module and <the / >operator

datetimeThe simple comparison operator <or >can be used to compare two dates. datetimeThe module provides timedeltamethods to operate on dates and times. timedelta()The methods receive the number of days as input and can perform operations on them.

A sample code is given below.

from datetime import datetime, timedelta

previous_date = datetime.now() - timedelta(days=1)
current_date = datetime.now()
print(present > past)

Output:

True

datetime.date()Compare two dates in Python using the

datetime.date()Can also be used to compare two dates. datetime.date()The method takes year, month, and day as input. Create two dates to compare and use a simple comparison operator to compare the two dates.

A sample code is given below.

import datetime

first_date = datetime.date(2020, 12, 16)
second_date = datetime.date(2015, 12, 16)

result = first_date < second_date
print(result)

Output:

False

timeCompare two dates in Python using the module

timeThe module provides strptimethe method to operate on dates. It takes a date in string format as input and converts it to Python's date format. After that, a simple comparison can be done to compare two dates.

Below is an example code.

import time

first_date = "30/11/2020"
second_date = "12/10/2019"

formatted_date1 = time.strptime(first_date, "%d/%m/%Y")
formatted_date2 = time.strptime(second_date, "%d/%m/%Y")
print(formatted_date1 > formatted_date2)

Output:

True

datetimeCompare two dates in Python using the module

datetimeThe module provides datetime()a method that takes three parameters and creates a date from year, month, and day. After getting the dates, you can use comparison operators to compare them.

A sample code is given below.

import datetime

# date in yy/mm/dd format
first_date = datetime.datetime(2020, 5, 11)
second_date = datetime.datetime(2020, 6, 10)

print("first date is greater than second_date: ", first_date > second_date)
print("first date is smaller than second_date: ", first_date < second_date)
print("first date is not equal to second_date: ", first_date != second_date)

Output:

first date is greater than second_date:  False
first date is smaller than second_date:  True
first date is not equal to second_date:  True

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Writing logs to a file in Python

Publish Date:2025/05/06 Views:132 Category:Python

This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file

If not statement in Python

Publish Date:2025/05/06 Views:123 Category:Python

The statement in Python if checks a specific condition and executes a block of code if the condition is true. if not The does the opposite of if the statement. It tests if a condition is false and then executes some statements. Using if not

Enumerating a dictionary in Python

Publish Date:2025/05/05 Views:98 Category:Python

The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()

Changing dictionary values in Python

Publish Date:2025/05/05 Views:109 Category:Python

This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary

Finding the maximum value in a Python dictionary

Publish Date:2025/05/05 Views:61 Category:Python

This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t

How to read input from stdin in Python

Publish Date:2025/05/05 Views:125 Category:Python

This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P

Maximum integer in Python

Publish Date:2025/05/05 Views:55 Category:Python

This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t

Get a list of time zones using Python

Publish Date:2025/05/05 Views:108 Category:Python

When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around

Convert NumPy array to list in Python

Publish Date:2025/05/05 Views:102 Category:Python

Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial