Have you ever ever puzzled what number of days it’s been because you began your weight-reduction plan, begun your every day jogs, or have been first working with Linux? For those who can keep in mind the beginning date, a easy Linux script can depend the times for you. All you want is the date command and a calculation that turns your dates into seconds, subtracts the beginning date from the tip date after which divides the consequence by the suitable quantity to show the seconds into the corresponding variety of days.
Why seconds?
The explanation for the conversion to days is that the date command can show a date because the variety of seconds because the begin of the Linux epoch—10=970-01-01. That is the one choice date supplies for wanting on the time distinction between two calendar dates.
For those who’d wish to see what number of seconds it’s been because the epoch started, enter a command like this:
$ date +%s 1655894738
As soon as the date command has transformed begin and finish dates to seconds, it’s simple to subtract one from the opposite and convert the consequence to the variety of days between the 2 dates.
Working the script
Right here’s the script which I name “count_the_days”.
#!/bin/bash echo -n “Enter begin date in yymmdd or yyyymmdd format: “ learn begin echo -n “Enter finish date in yymmdd or yyyymmdd format: “ learn finish echo $(( ($(date —date="$finish" +%s) - $(date —date="$begin" +%s) )/(60*60*24) ))
You’ll run the script like this:
$ count_the_days Enter begin date in yymmdd or yyyymmdd format: 220101 Enter finish date in yymmdd or yyyymmdd format: 220622 171
Word that the variety of days from the start of 2022 to June twenty second is 171. The 60*60*24 calculation turns the seconds into days by dividing the results of the subtraction by 86,400—the variety of seconds in a day as this expr command confirms:
$ expr 60 * 60 * 24 86400
Don’t fail to see that we are able to additionally use the script for dates sooner or later.
$ count_the_days Enter begin date in yymmdd or yyyymmdd format: 220101 Enter finish date in yymmdd or yyyymmdd format: 221231 364
For the reason that script is counting the variety of days from one date to the opposite, the consequence above is 364 somewhat than 365.
Years as 4-digit numbers
For those who occurred to note that the script’s prompts for coming into the dates help you specify years as 4-digit or as 2-digit numbers, you would possibly wish to strive that as properly. In reality, you can enter one date with a 4-digit yr and the opposite with a 2-digit yr. The date command will accommodate your desire and nonetheless provide the proper reply.
$ count_the_days Enter begin date in yymmdd or yyyymmdd format: 20220101 Enter finish date in yymmdd or yyyymmdd format: 220622 171
Reaching additional again in time
One factor that I solely not too long ago found is that the date command has no downside reaching again to dates means previous to the Linux epoch. For those who specify a date earlier than 1970-01-01 and ask to see it as various seconds, you merely get a really massive adverse quantity. Right here’s Dec 1, 1950 proven because the variety of seconds because the starting of the Linux epoch:
$ echo $(date —date="19501201" +%s) -602276400
That is the first purpose that the script encourages the usage of dates within the yyyymmdd format in addition to the extra generally used yymmdd format. It really works!
$ ./count_the_days Enter begin date in yymmdd or yyyymmdd format: 19000101 Enter finish date in yymmdd or yyyymmdd format: 20220101 44560
When you may not have anticipated that 2022 started 44,560 days after the yr 1900 started, this subsequent date calculation turns that quantity into years and shouldn’t shock you. Remember the fact that leap years aren’t thought-about, so we’d see a some decimal locations after the entire quantity if the expr command have been capable of present them.
$ expr 44560 / 365 122
Utilizing the bc command, you’ll be able to view the error or get a extra exact consequence like this:
$ echo “scale=2; 44560 / 365” | bc 122.08 $ echo “scale=2; 44560 / 365.25” | bc 121.99
Wrap-Up
You possible don’t spend a variety of time counting days, nevertheless it’s good to know that the date command can do this type of calculation for you. It’s additionally fascinating to understand how dates are saved on Linux methods and methods to work with them.
Copyright © 2022 IDG Communications, Inc.