>_ IzzyLab

Hands-on task

This task is step 11 of the course “Dates and times in the terminal”

Count the number of days between dates

medium~7 min

The file /root/range.txt contains two dates on two lines: the first is 2024-03-01, the second is 2024-06-01. Count by how many calendar days the second date is later than the first, and write the number to /root/days.txt. The sandbox's system time zone is Europe/Berlin, which has daylight saving time.

What you'll practise:
  • date
  • epoch
  • date arithmetic
  • bash arithmetic
  • sed

Topic breakdown

date has no direct arithmetic on dates, but there's a reliable trick: convert both dates to epoch seconds (format %s), and then work with ordinary numbers. The difference in seconds, divided by the number of seconds in a day (86400) via integer $(( )), gives the number of full days. Individual lines of the input file are conveniently pulled out with sed -n Np addressing.

What's checked

  1. number of days is correct

What it looks like