0

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Asanke
  • 551
  • 1
  • 11
  • 32
  • Possible duplicate of [How to find the difference in days between two dates?](https://stackoverflow.com/questions/4946785/how-to-find-the-difference-in-days-between-two-dates) – jww Apr 29 '18 at 21:23

1 Answers1

3

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.

Michael
  • 5,095
  • 2
  • 13
  • 35
  • in my case I get 0. `echo $(( ($(date +%s) - $(date +%s -ud '2003-08-02 17:24:33')) / 3600 / 24 )) 0 AG1> (date +%s -ud '2003-08-02 17:24:33') 1495436160 AG1> (date +%s -ud '2003-08-02 17:24:33') 1495436165 AG1> (date +%s -ud '2003-08-02 17:24:33') 1495436166` .... it looks like `(date +%s -ud '2003-08-02 17:24:33')` still gives the same number as `(date +%s) `, making the difference 0. – Asanke May 22 '17 at 06:56
  • 1
    Instead of `date`, try `/usr/gnu/bin/date` (http://www.unix.com/solaris/124464-date-d-illegal-option-solaris.html) – Michael May 22 '17 at 07:03
  • Also see here: https://stackoverflow.com/questions/17815327/equivalent-date-from-gnu-to-solaris – Michael May 22 '17 at 07:17
  • So the final solution is `echo $(( ($(date +%s) - $(/usr/gnu/bin/date +%s -ud '1980-01-01 00:00:00'))/3600/24)).` Didn't care much of the first date as it anyways provide the current date. – Asanke May 22 '17 at 09:00