Search code examples
shelldatesolaris

How to get the number of days since a specific date in bash


I need to find the number of days for today since a specific date (Lets say its 01011980). Wanted to write a shell scrip which will be running on solaris.

Ex: uname -a -->SunOS test-1 5.11 11.3 sun4v sparc sun4v

So far I have been able to get the current date formatted in to a desired format.

But could not find a way to convert a given date to a format which can be used in to subtract from the current date to get the number of days.

I tried date +%s and passing in the desired dates but every time it showed the crrent time.

AG1> date +%s
1495427876
AG1> (date +%s -ud '2003-08-02 17:24:33' )
1495427877
AG1>

I don't want to use another language like perl as this is going to execute different machines and additional programming language will be a impediment.

Can someone help.


Solution

  • Use arithmetic evaluation:

    echo $(( ($(date +%s) - $(date +%s -ud '2003-08-02 17:24:33')) / 3600 / 24 ))
    

    Answer: 5041.

    edit: since the question was about SunOS, date utility in this system differs from GNU date, and the key -d is not working. GNU version in SunOS is /usr/gnu/bin/date.