Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can you calculate the number of days since 1/1/1970? This is for updating the shadowLastChange attribute on OpenLDAP.

Is there a way to do this using the linux date command?

share|improve this question
    
In Bash and most modern Bourne-derived shells, $() is preferred over backticks for readability and other reasons. echo $(( $(date ...) / 86400 )) – Dennis Williamson Jan 10 '11 at 14:03
    
Don't have 2000 yet... can someone edit and fix the title :-) – ringø Jan 10 '11 at 14:32
up vote 15 down vote accepted

ring0 beat me by a few seconds, but the full command is:

echo $(($(date --utc --date "$1" +%s)/86400))

This goes by UTC time. Result:

root@hostname:~# echo $((`date --utc --date "$1" +%s`/86400))
14984

A quick check with WolframAlpha shows that this is the correct value.

share|improve this answer
    
thanks for the fairness ;-) – ringø Jan 14 '11 at 18:29
1  
what about leap seconds? :) – netvope Feb 21 '15 at 23:43
    
@netvope After an additional leap second, unix time is reset by one again. So each day adds exactly 86400 unix seconds. However, unix time 915148800 may stand for both UTC 1998-12-31T23:59:60 and 1999-01-01T00:00:00 en.wikipedia.org/wiki/Unix_time#Leap_seconds – Debilski Feb 1 at 12:14

The date command can give you the number of seconds since 1970-01-01 00:00:00 UTC.

  date +"%s"

You can divide the result by 3600*24 to get the number of days (UTC).

E.g. in Bash

  x=`date +"%s"` ; echo $(( $x / 3600 / 24 ))

to display the number of days.

share|improve this answer

I think this is the simplest method:

expr $(date +%s) / 86400
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.