Calculate Date Difference

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In Python, we can calculate the difference between two dates in days using the datetime module. This challenge will test your ability to write a function that takes two dates as input and returns the number of days between them.

Date Difference in Days

Write a function days_diff(start, end) that takes two date objects as input and returns the number of days between them. The function should subtract start from end and use datetime.timedelta.days to get the day difference.

def days_diff(start, end):
  return (end - start).days
from datetime import date

days_diff(date(2020, 10, 25), date(2020, 10, 28)) ## 3

Summary

In this challenge, you learned how to calculate the difference between two dates in days using the datetime module in Python. You also practiced writing a function that takes two dates as input and returns the number of days between them.