85

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    What 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
  • 3
    Are 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
  • 1
    thanks to all... but my system is not recognizing the +%s format specifier, am not getting the result :( –  Jul 07 '09 at 19:34
  • 3
    What 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 Answers5

172

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

NKSM
  • 5,422
  • 4
  • 25
  • 38
UberAlex
  • 3,477
  • 4
  • 22
  • 22
60

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:

  1. For the number of seconds since the Unix epoch use date(1) as follows:

    date +'%s'
    
  2. 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))
    
Community
  • 1
  • 1
Stephan202
  • 59,965
  • 13
  • 127
  • 133
  • 4
    This 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
12
echo $(($(date +%s) / 60 / 60 / 24))
cadrian
  • 7,332
  • 2
  • 33
  • 42
  • 2
    Note 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
  • 1
    Not all days have 86400 (60*60*24) seconds (DST, leap seconds, etc...) – Catfish_Man Jan 23 '13 at 08:19
  • 3
    Unix 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
2
echo `date +%s`/86400 | bc
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
-3

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.

mandroid
  • 2,308
  • 5
  • 24
  • 37