CS101 —Python exercise: counting days between two dates
Tags: logic, python, exercise, algorithms
Published: 2019-07-10 12:30:31

---
title: CS101 —Python exercise: counting days between two dates
published: true
tags: logic,python,exercise,algorithms
canonical_url: https://medium.com/@yairmorr/cs101-python-exercise-counting-days-between-two-dates-1197300c2f1d
---

### CS101 — Python exercise: counting days between two dates

![](https://cdn-images-1.medium.com/max/1024/1*x8me-NapICbyazKHYvhX9w.jpeg)

As part of Udacity’s [Intro to Computer Science online course](https://eu.udacity.com/course/intro-to-computer-science--cs101) that I’m taking, I ran into this interesting exercise. The goal was to create a function that takes two dates as input and counts the days between them. The first date is always earlier than the second. My solution was probably not the most elegant and my code could be shorter and more efficient. The logic however was the important thing to understand, and that is also what I want to focus on in this article. This exercise was written in Python.

If we put the two input dates on a timeline, the algorithm is working from the outside in. First it calculates the days in the months of each end of the timeline, then the days in each month for each year and then the days in all the years in between. The longer the distance is between the two input dates, the more conditions necessary for the algorithm to calculate the days correctly.

#### 1st step — Counting the days inside each month

{% gist https://gist.github.com/YairMorr/a01fe62775446c4e646fed2e8ad6297c %}

Here, _daysCount_ is the general counter of the days between the two days, which I used throughout my function and eventually what the function returned. I assigned it to the _day2_ argument. I then subtracted the amount of days in _month1_ from _day1_ to get the count of days for the rest of _month1_. I used the list _monthsCommonYear_ to get the exact amount of days in each month (I tend to leap years later on). This is the first because it occurs in every scenario.

This step returns the result for two input scenarios (I’m using DD/MM/YYYY format here):

1. The two input dates have the same year and same month (i.e. 12.8.2012–28.8.2012)
2. The two input dates have the same year but sequential months (12.8.2012–15.9.2012)

However, if the input dates are for the same year but the months are different, the function goes to the next step.

#### 2nd step — Same year, different month

{% gist https://gist.github.com/YairMorr/1a01037ee1743e683480f537031c9ad9.js %}

In this step, if the months are not sequential and the year is the same, I count the days between the first and the last month (i.e 12.8.2012–24.12.2012)

#### 3rd step — Sequential years

{% gist https://gist.github.com/YairMorr/5960fabd668252778bc32a79a4d2a132.js %}

Here, if the time between the input years is 1 year or more, I add whole months to my _daysCount_. for _year1_, I add the next month (_month1+1_) till the end of the year. For _year2_, I count months from (_month2–1_) till the beginning of the year. I take care not to count the days in the months I counted previously. I also take care to count the correct months in my _monthsCommonYear_ list, knowing my arguments can go from 1 to 12, but my list index goes from 0 to 11.

#### Distant years

{% gist https://gist.github.com/YairMorr/edb5ac7ca36ce0acded98a2615d3d76a %}

The last step is pretty straightforward. I add to my _daysCount_ the days in each year between _year1_ and _year2_. The function eventually returns _daysCount_.

#### Accounting for leap years

This exercise had an additional challenge, which made it even more interesting: the function has to tell if a year is common or leap and calculate the days accordingly. It was actually easier than I thought. The Udacity instructions linked to a [Wikipedia article](http://en.wikipedia.org/wiki/Leap_year#Algorithm) that offered the exact algorithm I needed and that was easily converted to a function like the one below.

{% gist https://gist.github.com/YairMorr/5d1c76cc3e53f1cc7ae121d3e9e48b5c %}

The comments in this snippet are taken straight from the description in Wikipedia.  
I used this function in two types of scenarios:

1. To determine which month list to iterate over — leap or common.
2. To calculate the amount of days in the last scenario — when there’s a difference of more than 1 year between the two input dates.

I called _checkLeapYear()_ 5 times in my function: 4 times to choose which month list to use (common or leap) and 1 time to add the years to my count. That’s why I preferred _checkLeapYear()_ to return a boolean instead of the number of days in each year (365 or 366).

Here’s the first part of the function, updated to calculate leap years. Notice I added another list for leap year months (the only difference is 29 days in February).

{% gist https://gist.github.com/YairMorr/d7305a23eb38a2736a5f9a4f03dca3de %}

The code I wrote for this exercise is long and encumbered, and perhaps later in my studies I will refactor it to something shorter and more efficient. The important thing for me here was exploring the logic behind the solution, rather than the way it was implemented.