Cozy Computer

Email logo Twitter logo Github Mark logo <——- Find me (Patrick McCarver) (they/he)

Counting days between two dates using Bash and Python 324 words (2 min)

Github code

TLDR: A Bash wrapper around Python’s date module for counting days between two dates.

./ "2020, 12, 21" "2020, 10, 12"
# 70 days, 0:00:00


I often want to know the number of days between two dates. Lately as the days get shorter, I want to know how many days until the Hiemal (Winter) Solstice, after which the length of days will begin increasing again.

It turns out you can just Google something like “how many days between 10/12/2020 and 12/21/2020”.

But we can find this programmatically using the date module from Python and then wrap it in a simple Bash script.

Code is a Bash script for counting the number of days between two dates:

#! /usr/bin/env bash
set -Ceuo pipefail

readonly DATE_ONE="$1"
readonly DATE_TWO="$2"

python -c "from datetime import date as d; print(d($DATE_ONE) - d($DATE_TWO))"

The python -c specifies a Python command to execute. We import the date library, use it to process our two input strings ($1, $2) into date objects, and then subtract them and print the result.


Provide two dates as "YYYY, MM, DD" where:

Date subtraction will return a positive number if the first date is later than the second date, or a negative number if the first date is smaller than the second date.

./ "2020, 12, 21" "2020, 10, 12"
# 70 days, 0:00:00

./ "2020, 10, 12" "2020, 12, 21"
# -70 days, 0:00:00

The script tells me that there are 70 days to go until the Winter Solstice!