Objective: Calculate or get the number of calendar days since 1970 on Linux.
The date
command can give us the number of seconds since 1970. Using this result, we can get the number of days by dividing the output by 86400
seconds.
1 2 3 4 5 |
$ date Thu, Aug 16, 2018 8:24:54 PM $ echo $(( $(date +%s) / 86400 )) 17759 |
The above output can be interpreted as: Number of days between 1-Jan-1970 and 16-Aug-2018 is 17759 days.
Note that bash
only supports integer arithmetic and not floating point by default. We can confirm this by performing a simple calculation as shown below.
1 2 |
$ echo $(( 5 / 2 )) 2 |
The answer is 2
and not 2.5
. The number will be rounded down to the nearest whole number.