How to get the current date value in epoch i.e., number of days elapsed since 1970-1-1. I need solution in unix shell script.
-
1What language or technology are you using? – Thomas Owens Jul 07 '09 at 19:26
-
I'm using unix. I need this to use inside a shell script... – Jul 07 '09 at 19:29
-
Today 14,432 days have passed since the epoch. ;) http://www.timeanddate.com/date/durationresult.html?m1=1&d1=1&y1=1970&m2=&d2=&y2= – Stephan202 Jul 07 '09 at 19:29
-
3Are you sure you want the number of _days_ since epoch? The answers so far give you seconds :) you'll need to divide that by 60 * 60 * 24 to get your answer :) – Jeremy Smyth Jul 07 '09 at 19:29
-
Search for "days between two dates". – Stephan202 Jul 07 '09 at 19:29
-
1thanks to all... but my system is not recognizing the +%s format specifier, am not getting the result :( – Jul 07 '09 at 19:34
-
3What kind of system are you on, then? Anyway, give the solution I posted below a try. Perhaps that script is more portable... – Stephan202 Jul 07 '09 at 19:54
-
Regarding @Stephan202's link, I find [Epoch Converter](http://www.epochconverter.com/) to be a richer source of information and tools than timeanddate.com. – Randall Cook Nov 07 '11 at 22:51
5 Answers
The Unix Date command will display in epoch time
the command is
date +"%s"
https://linux.die.net/man/1/date
Edit: Some people have observed you asked for days, so it's the result of that command divided by 86,400
-
7Note that `%s` is an extension. POSIX date does not have `%s`. See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details. – Jens Sep 01 '11 at 08:04
-
1
-
5Again, in unix time (date +%s) each day has 86400 "seconds", it does not include leap seconds and certainly not DST. The answer is correct. – Sam Watkins Oct 23 '13 at 00:57
-
2
-
-
That won't give you the actuall millisecond current time though (which I'm looking for in my performance profiling). – Sridhar Sarnobat Aug 28 '21 at 03:38
Update: The answer previously posted here linked to a custom script that is no longer available, solely because the OP indicated that date +'%s'
didn't work for him. Please see UberAlex' answer and cadrian's answer for proper solutions. In short:
For the number of seconds since the Unix epoch use
date(1)
as follows:date +'%s'
For the number of days since the Unix epoch divide the result by the number of seconds in a day (mind the double parentheses!):
echo $(($(date +%s) / 60 / 60 / 24))

- 1
- 1

- 59,965
- 13
- 127
- 133
-
4This is not the best answer, and no longer useful at all, as the links are broken. It should be a one liner, as below. – Sam Watkins Oct 23 '13 at 00:59
-
3@SamWatkins: Very much agreed. Unfortunately accepted answers cannot be deleted. – Stephan202 Oct 23 '13 at 12:44
-
1@alancnet: the answer was not incorrect for the OP, but he did have a very weird setup indeed. Anyway, I had enough of all the downvotes, so I rewrote the answer to something that is useful for the other 99.9999% of the planet. – Stephan202 Apr 14 '16 at 06:35
echo $(($(date +%s) / 60 / 60 / 24))

- 7,332
- 2
- 33
- 42
-
2Note that `%s` is an extension. POSIX date does not have `%s`. See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details. – Jens Sep 01 '11 at 08:05
-
1Not all days have 86400 (60*60*24) seconds (DST, leap seconds, etc...) – Catfish_Man Jan 23 '13 at 08:19
-
3Unix time does have 86400 seconds per day exactly. Note, if using with -d for a specific date, suggest -u for UTC, or answer will vary by timezone. echo $(( $(date -u -d '2014-01-01' +%s) / 86400 )) – Sam Watkins Oct 23 '13 at 00:56
Depending on the language you're using it's going to be something simple like
CInt(CDate("1970-1-1") - CDate(Today()))
Ironically enough, yesterday was day 40,000 if you use 1/1/1900 as "day zero" like many computer systems use.

- 2,308
- 5
- 24
- 37
-
That looks like VB to me. The question asks in relation to a Unix shell script. – Noldorin Jul 07 '09 at 19:32
-
2