Date difference between 2 dates in 'yyyy-mm-dd hh:mm:ss' format

Hi all,

I know this may have already been asked but hey ho...

i have two dates in the 'yyyy-mm-dd hh:mm:ss' format.

'2009-01-03 01:00:00'
'2009-04-05 00:00:00'

How can i, in shell script determine their differences?

Please note, the time may not be available, so please suggest both ways as i may have dates in this format too:

i.e. for when i only have:

'2009-01-03'
'2009-04-05'

Go here and look in the sample chapter for diffdate. (Hi Chris)
[url="Shell Scripting Recipes: A Problem-Solution Approach, by Chris F.A. Johnson"]

You can find it at:

This might be the key command you need:

date +%s -d"2009-01-03"

will return seconds since epoch
do that twice, once for each time, storing to variables
then math will give you # seconds between two dates
finally, divide by seconds in a day
voila -- # days between two dates

I have two dates with me, and i want the difference of these two days in form of days.
what will be the code for that ?

Example:
06-Apr-2009,04-Mar-09