views:

2951

answers:

5

Hi, 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.

A: 

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
That looks like VB to me. The question asks in relation to a Unix shell script.
Noldorin
He edited it after I answered. Thanks for your input anyway.
mandroid
+2  A: 
echo $(($(date +%s) / 60 / 60 / 24))
cadrian
+1 Should work on most bourne shells and unixes
nos
+1  A: 

You mention you want a UNIX shell script. Peter J. Acklam wrote some shell scripts that may help you. Look here:

$ ./gymd2uday.sh
14432

$ echo $(( $(./gymd2uday.sh 1985 02 20) - $(./gymd2uday.sh 1970 1 1) )) 
5529

As you can see, the default date is the current date. Peter's script does not use the %s format specifier, so it may work for you.

Stephan202
It works... thanks a lot.
You're welcome :)
Stephan202
A: 

The Unix Date command will display in epoch time

the command is

date +"%s"

http://unixhelp.ed.ac.uk/CGI/man-cgi?date

Edit: Some people have observed you asked for days, so it's the result of that command divided by 86,400

UberAlex
A: 
echo `date +%s`/86400 | bc
Chris Doggett