Linux: Get Number of Days since 1970

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.

$ 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.

$ echo $(( 5 / 2 ))
2

The answer is 2 and not 2.5. The number will be rounded down to the nearest whole number.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }