94

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.

8
  • 1
    What language or technology are you using? Commented Jul 7, 2009 at 19:26
  • I'm using unix. I need this to use inside a shell script... Commented Jul 7, 2009 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 :) Commented Jul 7, 2009 at 19:29
  • 1
    thanks to all... but my system is not recognizing the +%s format specifier, am not getting the result :( Commented Jul 7, 2009 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... Commented Jul 7, 2009 at 19:54

5 Answers 5

182

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

Sign up to request clarification or add additional context in comments.

6 Comments

Note that %s is an extension. POSIX date does not have %s. See pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details.
Not all days have 86400 seconds (DST, leap seconds, etc...)
Again, in unix time (date +%s) each day has 86400 "seconds", it does not include leap seconds and certainly not DST. The answer is correct.
Is it possible to display the milliseconds since the epoch?
That won't give you the actuall millisecond current time though (which I'm looking for in my performance profiling).
|
62

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))
    

3 Comments

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.
@SamWatkins: Very much agreed. Unfortunately accepted answers cannot be deleted.
@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.
12
echo $(($(date +%s) / 60 / 60 / 24))

3 Comments

Note that %s is an extension. POSIX date does not have %s. See pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details.
Not all days have 86400 (60*60*24) seconds (DST, leap seconds, etc...)
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 ))
2
echo `date +%s`/86400 | bc

1 Comment

Not all days have 86400 seconds (DST, leap seconds, etc...)
-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.

2 Comments

That looks like VB to me. The question asks in relation to a Unix shell script.
He edited it after I answered. Thanks for your input anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.