Get current time in seconds since the Epoch on Linux, Bash


Question

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds.

date doesn't seem to offer that option. Is there an easy way?

1
546
3/5/2018 8:59:09 PM

Accepted Answer

This should work:

date +%s
1006
7/7/2009 2:25:00 PM

Just to add.

Get the seconds since epoch(Jan 1 1970) for any given date(e.g Oct 21 1973).

date -d "Oct 21 1973" +%s


Convert the number of seconds back to date

date --date @120024000


The command date is pretty versatile. Another cool thing you can do with date(shamelessly copied from date --help). Show the local time for 9AM next Friday on the west coast of the US

date --date='TZ="America/Los_Angeles" 09:00 next Fri'

Better yet, take some time to read the man page http://man7.org/linux/man-pages/man1/date.1.html


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon