Number of seconds since midnight till now in unix

Image of Clock

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

About author

Love coding, crazy about logic, like solution architecting and solving problems. Thirsty for knowledge, challenges & most importantly innovation. One of the passionate brain behind SpryOX

Leave a Reply

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