Could somebody recommend a small utility to calculate the number of days between two dates?

Wybo Dekker wybo at servalys.nl
Sun Jan 31 22:04:44 UTC 2010


Werner Schram wrote:
> On 01/31/2010 06:46 PM, Peng Yu wrote:
>> Could somebody let me what tool I can use to calculate the number of
>> days before two dates in ubuntu?
>>
>>    
> This one only works in bash:
> 
> echo $(((`date +%s -d "jan 31 00:00:00 UTC 2010"`-`date +%s -d "jan 30 
> 00:00:00 UTC 2010"`) / 86400))

My script, using this:

#!/bin/bash

# days - print difference between two dates in days

function usage {
   echo 'Usage date1 [date2]'
   echo 'returns date1 - date2 (in days)'
   echo 'default for date2 is now'
   echo 'date formats: 2010-12-03 20101203 now today yesterday tomorrow'
   exit
}

test "$1" = "" && usage
recent=$1
past=$2
test "$past" = "" && past=now

TZ=UTC echo $(((`date +%s -d $recent` - `date +%s -d $past`) / 86400))

-- 
Wybo Dekker




More information about the ubuntu-users mailing list