4

I would like to create a script that tells you how many seconds, minutes and hours have passed since a date (much like the +%s date command format). How can I subtract the current date with a specific date? For example: how many seconds, minutes and hours have passed since 4th of July 1776.

  • The Perl module Date::Calc makes this easy, if you know Perl. – waltinator Mar 16 '17 at 19:46
4

Here's one way to do it:

time=$(($(date -d"1776-07-04 00:00:00" +%s) - $(date +%s)))

sets a time variable you can use like so:

echo $time
-7595723059

The +%s tells date to format to seconds since 1970-01-01 00:00:00 UTC which helps give us a starting point for doing the math in seconds.

Now it can also be added to a script with the same variables.

:~$ time=$(($(date -d"1776-07-04 00:00:00" +%s) - $(date +%s)))
:~$ printf '%dh:%dm:%ds\n' $(($time/3600)) $(($time%3600/60)) $(($time%60))
-2109923h:-12m:-55s

If you want Years:Days:Hours:Minutes:Seconds since, it would be the following:

printf '%dy:%dd:%dh:%dm:%ds\n' $(($time/60/60/24/365)) $(($time/60/60/24%365)) $(($time/3600%24)) $(($time%3600/60)) $(($time%60))

Which would give you output like the following:

:~$ time=$(($(date -d"1776-07-04 00:00:00" +%s) - $(date +%s)))
:~$ printf '%dy:%dd:%dh:%dm:%ds\n' $(($time/60/60/24/365)) $(($time/60/60/24%365)) $(($time/3600%24)) $(($time%3600/60)) $(($time%60))
-242y:-11d:-7h:-46m:-37s

Hope this helps!

  • And how do I subtract 4th of July 1776 with the current date? I tried $(($(date -d "1776-07-04 00:00:00") - $(date))) but it didn't work for me. – user661429 Mar 16 '17 at 18:32
  • @Cytotlewo You need to format the date into something that the math will understand. Adding the +%s gives each date a reference point of seconds since 1970-01-01 00:00:00 UTC – Terrance Mar 16 '17 at 18:49
  • Oh wait, I think I got it. If I would like to know how many seconds have passed since 1776-07-04, I could subtract how many seconds have passed since 1970 with how many seconds have passed between 1970 and the specific date. So, it would be: $(($(date +%s) - $(date '+%s' -d $1776-07-04))) Correct me if I'm wrong. And if I would like to know how many minutes have passed, I'd have to divide it by 31536000 to get an approximate number. Sorry, I'm a slow thinker when it comes to these things. – user661429 Mar 16 '17 at 19:44
  • @Cytotlewo Minutes would just be the number divided by 60 /60as there are 60 seconds in every minute. To figure out how many hours it would be divided by 60 then 60 again /60/60, since there are 60 seconds in 1 minute and 60 minutes in 1 hour. For days it would be divided by 60 then 60 and then 24 /60/60/24. So, taking my first part that is time=$(($(date -d"1776-07-04 00:00:00" +%s) - $(date +%s))) to figure out the number of days it would be days=$(($time/60/60/24)) then echo $days should show -87913. – Terrance Mar 16 '17 at 19:55
  • Ohh, silly me. My subtraction was to figure out how many years have passed, since I divided it by the total seconds of a year (or am I wrong again? lol). – user661429 Mar 16 '17 at 20:09
1

If you can live with a dependency, the task is elegantly expressed using dateutils:

$ datediff 1776-07-04T00:00:00 now -f '%H:%0M:%0S'
2111284:08:34

Disclaimer: I am the author of the package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy