Python Exercise: Calculate number of days between two dates


(Python Example for Citizen Data Scientist & Business Analyst)


Write a Python program to calculate number of days between two dates.

Python, month, day) :

The function returns date object with same year, month and day. All arguments are required. Arguments may be integers, in the following ranges:

  • MINYEAR <= year <= MAXYEAR
  • 1 <= month <= 12
  • 1 <= day <= number of days in the given month and year


If an argument outside those ranges is given, ValueError is raised.

  • Note: The smallest year number allowed in a date or datetime object. MINYEAR is 1.
  • The largest year number allowed in a date or datetime object. MAXYEAR is 9999.



Sample Solution:-

Python Code:

from datetime import date
f_date = date(2014, 7, 2)
l_date = date(2014, 7, 11)
delta = l_date - f_date

Sample Output:



Calculate number of days between two dates


Sign up to get end-to-end “Learn By Coding” example.



Applied Machine Learning & Data Science with Python, R and SQL.

A list of Python, R and MATLAB Codes for Applied Machine Learning and Data Science


Subscribe to our YouTube channel and press the ring to get latest update on Python and R coding recipesCoding is our strength.


Learn by Coding Categories:


Data Analytics:

Data Science:

Data Visualisation:

Machine Learning Recipe:




Supervised Learning:

Tabular Data Analytics:

End-to-End Data Science Recipes:

Applied Statistics:

Bagging Ensemble:

Boosting Ensemble:



Data Analytics:

Data Science:

Data Visualisation:

Decision Tree:


Machine Learning Recipe:

Multi-Class Classification:

Neural Networks:

Python Machine Learning:

Python Machine Learning Crash Course:

R Classification:

R for Beginners:

R for Business Analytics:

R for Data Science:

R for Data Visualisation:

R for Excel Users:

R Machine Learning:

R Machine Learning Crash Course:

R Regression:



Excel examples for beginners:

C Programming tutorials & examples:

Javascript tutorials & examples:

Python tutorials & examples:

R tutorials & examples:

SQL tutorials & examples:

SQL for Beginners in 2 Weeks:


Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R @



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.