Number of seconds since midnight till now in unix

Had a requirement to find out the number of seconds since midnight till now in unix.

Basically the requirment was more to this, it was to find the latest file created after midnight having specific pattern and to search certain pattern in it ūüėČ nice one right……

Anyway splitting the blog to provide you the code to find out the number of seconds past midnight and solution is as below command

Number of second past midnight till now

echo $(( ($(date “+%s”) – $(date -d ‘today 00:00:00’ “+%s”)) ))

Number of minutes past midnight till now

echo $(( ($(date “+%s”) – $(date -d ‘today 00:00:00’ “+%s”))/60 ))

 

Explanation :

$(date “+%s”) : Gives current seconds¬†elapsed since¬†January 1, 1970 (Unix epoch)

$(date -d ‘today 00:00:00’ “+%s”) : Gives seconds at midnight elapsed since¬†January 1, 1970

$(( ($(date “+%s”) – $(date -d ‘today 00:00:00’ “+%s”)) )) : Difference of both seconds

$(( ($(date “+%s”) – $(date -d ‘today 00:00:00’ “+%s”))/60 )) :¬†Difference of both seconds and divide by 60 to get minutes

Vikram Pawar

Vikram Pawar

Coder by passion..... Thirsty for knowledge, challenges & most importantly innovation. Working with BNP Paribas

You may also like...

1 Response

  1. December 14, 2015

    […] Find number of minutes¬†since midnight. This is explained in another post¬†Number of seconds since midnight till now […]

Leave a Reply

Your email address will not be published. Required fields are marked *