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

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?


这应该工作:

date +%s

Just to add.

Get the seconds since epoch(Jan 1 1970) for any given date(eg 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://www.manpages.info/linux/date.1.html


So far, all the answers use the external program date .

Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat , handled by strftime(3) ( man 3 strftime for informations about the formats).

So, for a pure Bash solution:

printf '%(%s)Tn' -1

or if you need to store the result in a variable var :

printf -v var '%(%s)T' -1

No external programs and no subshells!

Since Bash 4.3, it's even possible to not specify the -1 :

printf -v var '%(%s)T'

(but it might be wiser to always give the argument -1 nonetheless).

If you use -2 as argument instead of -1 , Bash will use the time the shell was started instead of the current date (but why would you want this?).

链接地址: http://www.djcxy.com/p/18530.html

上一篇: JDE Julian Date格式的精确定义是什么?

下一篇: 从Epoch on Linux,Bash开始,立即获得当前时间