UNIX- Adding days to datetime variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX- Adding days to datetime variable
# 1  
Old 12-12-2014
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.
# 2  
Old 12-12-2014
What is your OS, shell, date version?
# 3  
Old 12-12-2014
It is AIX version 6.
# 4  
Old 12-12-2014
Quote:
Originally Posted by Nikita1981
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.
It would be a breeze if you know perl and it is installed on that machine...
# 5  
Old 12-12-2014
The short answer is no. You cannot write shell script command to do date arithmetic.

days elapsed between 2 dates

This is code perderabo put together specifically to answer date questions.
# 6  
Old 12-13-2014
Hi.

Some possibilities:
Code:
	
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)
	   ( https://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)
	   ( https://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

Last edited by drl; 12-14-2014 at 01:53 PM.. Reason: Add version for ksh (thanks Don Cragun)
# 7  
Old 12-13-2014
Quote:
Originally Posted by drl
Hi.

Some possibilities:
Code:
	
Date, time arithmetic, math
	...
	2) ksh printf
	...

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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX help adding data to end of a line with a variable

I am trying to add a date variable to the end of each line. This is what I have to start with cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0}' >> ${DATAPATH}/badparticipant.csv This is what I tried $DATE is a variable I have defined. cat... (3 Replies)
Discussion started by: req62861
3 Replies

2. Shell Programming and Scripting

Adding days to date

Hi, please can somebody let me know the easiest way to add days to a date. I can do this in perl but would like to able to do it in a shell script. Desired output would be: date +'%Y-%m-%d' + 10 = 2016-05-02 Thank you (8 Replies)
Discussion started by: andy391791
8 Replies

3. Shell Programming and Scripting

Adding 48 hours to DateTime

Hey Guys, I have looked for a solution throughout the forum for my particular question, but I cant find one. So I'm sorry if I overlooked it. I need to be able to 48 add hours to a particular DateTime string. I have a variable named $startTime I would like to be able to take that... (1 Reply)
Discussion started by: chagan02
1 Replies

4. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

5. Windows & DOS: Issues & Discussions

Adding or subtracting days from current date in batch script

Hi, I'm writing an batch file to create report In the batch file iam passing two arguments:startdate and finishdate Ex: startdate=07-sep-2009 finishdate=07-sep-2011 I need to have script that takes command line argument as input and gives me out currentdate last year and current date... (2 Replies)
Discussion started by: anand1773
2 Replies

6. Shell Programming and Scripting

Subtract days from a variable holding date

Hi, could someone help on this.. I have a date in variable procdate="05/30/2009" I would want to Subtract it with 3 or 4 (2 Replies)
Discussion started by: infernalhell
2 Replies

7. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

8. Shell Programming and Scripting

Add minutes in Datetime Variable in Unix

Hi, New to scripting. I have datetime as a String and I need to add some hours/minutes in to that. 21/10/2009 23:00:00 After adding 180 minutes; it will look like this 22/10/2009 02:00:00 Regards, Vikas Mahajan (9 Replies)
Discussion started by: vikas482
9 Replies

9. Shell Programming and Scripting

Adding days to an input date.

Hello Unix gurus, I need to add days to the input date and further use it in comparision with the existing date. Im having issues sto add days to date,can you guys help me with script or function with which I can add days to the date. Thanks, Sud (10 Replies)
Discussion started by: sud
10 Replies

10. UNIX for Dummies Questions & Answers

adding or subtracting days in the o/p of date

how can we add or subtract days from the output of date command in unix... like if i want to subtract a day from the result of date command like this.. v_date=`date +%Y%m%d` this wud give me 20080519 now i want to subtract one day from this.. so tht it wud give me 20080518.. how do i do... (1 Reply)
Discussion started by: St.Fartatric
1 Replies
Login or Register to Ask a Question