Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How to print in linux/unix bash the number of days since a specific date till now? (for example days since 23rd of June 2009 till today)!

share|improve this question
    
In what format is the "first" date? Can we choose? –  Bittrance Jun 13 '11 at 11:23

2 Answers 2

You can use something like this:

date1=$(date --utc --date "2008-10-20" +%s)
date2=$(date --utc --date "2009-10-20" +%s)
diffdays=$(( (date2-date1)/(3600*24) ))

This assumes date2 is more recent than date1.

Adapted from http://www.unix.com/tips-tutorials/31944-simple-date-time-calulation-bash.html.

share|improve this answer

I prefer languages with specialized datetime libraries for date arithmetic. For example:

ruby -r date -e 'd = Date.parse(ARGV.shift); p (Date.today - d).to_i' 2010-09-23
share|improve this answer
    
Is ruby installed by default on OSX? –  jftuga Jun 13 '11 at 15:26

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.