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– Thomas Owens2009-07-07 19:26:11 +00:00Commented Jul 7, 2009 at 19:26
-
I'm using unix. I need this to use inside a shell script...Krishna– Krishna2009-07-07 19:29:02 +00:00Commented Jul 7, 2009 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– Jeremy Smyth2009-07-07 19:29:39 +00:00Commented Jul 7, 2009 at 19:29
-
1thanks to all... but my system is not recognizing the +%s format specifier, am not getting the result :(Krishna– Krishna2009-07-07 19:34:22 +00:00Commented Jul 7, 2009 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– Stephan2022009-07-07 19:54:21 +00:00Commented Jul 7, 2009 at 19:54
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
6 Comments
%s is an extension. POSIX date does not have %s. See pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details.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))
3 Comments
echo $(($(date +%s) / 60 / 60 / 24))
3 Comments
%s is an extension. POSIX date does not have %s. See pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html for details.echo `date +%s`/86400 | bc
1 Comment
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.