Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

Python 3 Datetime Module Script to Compare and Sort Two Dates Full Example For Beginners



Welcome folks today in this blog post we will be comparing two given dates using the built in module called datetime in python. All the full source code of the application is given below.



Get Started



In order to get started you need to create an app.py file and copy paste the following code




# Simple Python program to compare dates 

# importing datetime module 
import datetime 

# date in yyyy/mm/dd format 
d1 = datetime.datetime(2018, 5, 3) 
d2 = datetime.datetime(2018, 6, 1) 

# Comparing the dates will return 
# either True or False 
print("d1 is greater than d2 : ", d1 > d2) 
print("d1 is less than d2 : ", d1 < d2) 
print("d1 is not equal to d2 : ", d1 != d2)


READ  Python 3 Instagramy + Pandas Script to Scrape Instagram Users Followers and Following Usernames and Posts Data and Analyze them Full Project For Beginners


Now if you execute the python script by typing the below command as shown below


python app.py




Sorting Two Dates


READ  Python 3 Script to Find Prime Numbers From 1 to 100 Using For loop Full Project For Beginners


Now we will look at an example to sort two given dates in ascending or descending order as shown below




# Python program to sort the dates 

# importing datetime module 
from datetime import *

# create empty list 
group = [] 

# add today's date 

# create some more dates 
d = date(2015, 6, 29) 

d = date(2011, 4, 7) 

# add 25 days to the date 
# and add to the list 
group.append(d + timedelta(days = 25)) 

# sort the list 

# print the dates 
for d in group: 


READ  Python 3 Pillow Image Library to Convert Images of JPG PNG and Webp Formats Full Tutorial For Beginners



Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Coding Shiksha 2020. All Rights Reserved.
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :