UNIX- Adding days to datetime variable

Hello All,

I have a date with timestamp stored in a variable and another variable which stores no. of days.

Var1=2014-12-11 12:54:21
Var2=5

In my unix script, I have to add the no. of days stored in var2 with the var1 date.
Var3=var1+var2=2014-12-16 12:54:21

Can you please advise if there is any unix function similar to dateadd MS SQL Server function. I tried using date -d argument,
but it is not supported in our environment.

Thank you in advance for your valuable suggestions.

What is your OS, shell, date version?

It is AIX version 6.

It would be a breeze if you know perl and it is installed on that machine...

The short answer is no. You cannot write shell script command to do date arithmetic.

http://www.unix.com/unix-for-dummies-questions-and-answers/4870-days-elapsed-between-2-dates.html\#post16559

This is code perderabo put together specifically to answer date questions.

Hi.

Some possibilities:

	
Date, time arithmetic, math
	1) gnu date

	2) ksh (version 93, not 88) printf

	3) date.pl (limited arith, but "date.pl -d '- 1 day'" works)
	   ( http://www.unix.com/tips-tutorials/
	   239167-general-purpose-date-script.html)

	4) dconv et al, e.g. ddiff
	   ( dateutils: http://www.fresse.org/dateutils/ )

	5) tm2tm (, OK in 32-bit; fails to compile in 64-bit)
	   ( http://www.unix.com/shell-programming-scripting/
	   146216-date-difference-between-freebsd-linux.html#post302463136 )

	6) perl custom (with function strftime)

	7) date-cpan.pl, cpan perl date
	   ( http://cpansearch.perl.org/src/CWEST/ppt-0.14/
	   src/date/date.jgross )

Best wishes ... cheers, drl

I believe the default ksh on AIX is a ksh88 version. To do date arithmetic with the ksh printf , you need a 1993 or later version of ksh . I believe many AIX systems have a another Korn shell installed as ksh93 that will do what is wanted here.

1 Like

I'd be most interested if the ksh93 installed on AIX version 6 supports the %T printf format. Certainly in AIX 5.3, which comes with a much older ksh93 (version ksh93n I believe) this wasn't supported.

@Nikita1981, Please report if this command works for you:

$ ksh93 -c 'printf "%T\n" "Dec 14 11:59"'

Thank you for all your valuable suggestions. I am planning to use database to get this done.

@Chubler_XL: No, the command doesn't work.