Counting the days on Linux


Have you ever wondered how many days it’s been since you started your diet, begun your daily jogs, or were first working with Linux? If you can remember the start date, a simple Linux script can count the days for you. All you need is the date command and a calculation that turns your dates into seconds, subtracts the start date from the end date and then divides the result by the appropriate number to turn the seconds into the corresponding number of days.

Why seconds?

The reason for the conversion to days is that the date command can display a date as the number of seconds since the start of the Linux epoch—10=970-01-01. This is the only option date provides for looking at the time difference between two calendar dates.

If you’d like to see how many seconds it’s been since the epoch began, enter a command like this:

$ date +%s
1655894738

Once the date command has converted start and end dates to seconds, it’s easy to subtract one from the other and convert the result to the number of days between the two dates.

Running the script

Here’s the script which I call “count_the_days”.

#!/bin/bash

echo -n “Enter start date in yymmdd or yyyymmdd format: “
read start
echo -n “Enter end date in yymmdd or yyyymmdd format: “
read end
echo $(( ($(date —date="$end" +%s) - $(date —date="$start" +%s) )/(60*60*24) ))

You would run the script like this:

$ count_the_days
Enter start date in yymmdd or yyyymmdd format: 220101
Enter end date in yymmdd or yyyymmdd format: 220622
171

Note that the number of days from the beginning of 2022 to June 22nd is 171. The 60*60*24 calculation turns the seconds into days by dividing the result of the subtraction by 86,400—the number of seconds in a day as this expr command confirms:

$ expr 60 * 60 * 24
86400

Don’t fail to notice that we can also use the script for dates in the future.

$ count_the_days
Enter start date in yymmdd or yyyymmdd format: 220101
Enter end date in yymmdd or yyyymmdd format: 221231
364

Since the script is counting the number of days from one date to the other, the result above is 364 rather than 365.

Years as 4-digit numbers

If you happened to notice that the script’s prompts for entering the dates allow you to specify years as 4-digit or as 2-digit numbers, you might want to try that as well. In fact, you could enter one date with a 4-digit year and the other with a 2-digit year. The date command will accommodate your preference and still give you the right answer.

$ count_the_days
Enter start date in yymmdd or yyyymmdd format: 20220101
Enter end date in yymmdd or yyyymmdd format: 220622
171

Reaching further back in time

One thing that I only recently discovered is that the date command has no problem reaching back to dates way prior to the Linux epoch. If you specify a date before 1970-01-01 and ask to see it as a number of seconds, you simply get a very large negative number. Here’s Dec 1, 1950 shown as the number of seconds since the beginning of the Linux epoch:

$ echo $(date —date="19501201" +%s)
-602276400

This is the primary reason that the script encourages the use of dates in the yyyymmdd format as well as the more commonly used yymmdd format. It works!

$ ./count_the_days
Enter start date in yymmdd or yyyymmdd format: 19000101
Enter end date in yymmdd or yyyymmdd format: 20220101
44560

While you might not have anticipated that 2022 began 44,560 days after the year 1900 began, this next date calculation turns that number into years and shouldn’t surprise you. Keep in mind that leap years aren’t considered, so we’d see a some decimal places after the whole number if the expr command were able to provide them.

$ expr 44560 / 365
122

Using the bc command, you can view the error or get a more precise result like this:

$ echo “scale=2; 44560 / 365” | bc
122.08
$ echo “scale=2; 44560 / 365.25” | bc
121.99

Wrap-Up

You likely don’t spend a lot of time counting days, but it’s nice to know that the date command can do this kind of calculation for you. It’s also interesting to know how dates are stored on Linux systems and how to work with them.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2022 IDG Communications, Inc.



Source link