[Solved] Question for Perderabo on date calc

Hi,
First of all, thanks for all the awesome suggestions on this forum. This helps all the UNIX enthusiast like me.

Now, I had a similar requirement as mentioned in a very old post here:

Question about Perderabo's "Days Elapsed Between Two Dates"

But I am struggling what to change in the following to give me the month as MM always. At the moment it is truncating the 0 from single digit months.

E.g.
20130425 instead of 2013425

Thanks!

Instead of modifying Perderabo's script, you can use printf formatting option to zero lpad:

$ ./datecalc -a 2003 10 01 - 1 | read y m d
$ printf "%d%02d%02d\n" $y $m $d
20030930

Yoda - Thanks for your prompt reply. But I am not sure if I understand correctly.

Can I use the printf command as an conjuction to Perderabo's script's output?

Basically, the reason why I am using Perderabo's script is to find out the date which is 21 days older than system date in the format of YYYYMMDD. I would really appreciate if there is an easier way to do so without using the Perderabo's script.

P.S. I am using KSH on AIX 6.1.

OK, modify line number 188 to:

177          ((standard_jd=jd+2400001))
178          ((temp1 = standard_jd + 68569))
179          ((temp2 = 4*temp1/146097))
180          ((temp1 = temp1 - (146097 * temp2 + 3) / 4))
181          ((year  = 4000 * (temp1 + 1) / 1461001))
182          ((temp1 = temp1 - 1461 * year/4 + 31))
183          ((month = 80 * temp1 / 2447))
184          ((day   = temp1 - 2447 * month / 80))
185          ((temp1 = month / 11))
186          ((month = month + 2 - 12 * temp1))
187          ((year  = 100 * (temp2 - 49) + year + temp1))
188          printf "%d%02d%02d\n" $year $month $day
189          return 0

Here is the output after modification:

./datecalc -a 2003 10 01 - 1
20030930

You could always get coreutils (provides the linux date command and many other things) from the IBM toolbox for linux applications.

I have a heap of scripts on AIX 5.3 that do date manipulation using this technique.

Yoda - Thank you very much for your help. This works just perfectly fine! :b: