LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2009, 01:33 PM   #1
ne00
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Rep: Reputation: 0
trouble adding days to a passed in date string


I'm having trouble figuring out how to use the date command to accept a passed in date string and adding 10 days to it.

I tried the following and it works fine:

Code:
completedate=$(date -d '2009-03-27 + 10 days' +%m-%d)

echo $completedate
Output is 04-06

But when I try to pass in a date string that's entered in from the prompt, I'm having trouble getting it to work.

Code:
read -p "enter date: " xdate

completedate=$(date -d '($xdate) + 10 days' +%m-%d)

echo $completedate
The output is basically taking today's date and adding 10 days to it and ignoring the input date which is 2009-03-27.

Thanks

Dave
 
Old 04-08-2009, 01:36 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Remove the unnecessary parentheses around ($xdate) and use double quotes instead of single quotes. The single ones prevent the shell to do the correct variable substitution.
 
Old 04-08-2009, 01:39 PM   #3
kolargol
Member
 
Registered: Jan 2003
Posts: 56

Rep: Reputation: 3
chabge ($xdate) to $xdate and it should work
 
Old 04-08-2009, 03:09 PM   #4
ne00
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for the reply, I gave those a try and still can't get it to work.

For the use of double quotes I tried...

Code:
completedate=$(date -d '"$xdate" + 10 days' +%m-%d)
and

Code:
completedate=$(date -d "$xdate + 10 days" +%m-%d)
Both I received an invalid date error.

and for removing the ()...

Code:
completedate=$(date -d '$xdate + 10 days' +%m-%d)
I received an invalid date error for 'xdate + 10 days'

any other suggestions I can try?
 
Old 04-08-2009, 03:22 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The second of your examples should work:
Code:
$ read -p "enter date: " xdate
enter date: 2009-03-27
$ completedate=$(date -d "$xdate + 10 days" +%m-%d)
$ echo $completedate
04-06
The other ones are wrong because of the single quotes around the date string. In that case the string $xdate is interpreted literally, since the single quotes do not let the shell substitute the value to the variable. You can also remove the plus sign, even if I think that's not a problem:
Code:
date -d "$xdate 10 days" +%m-%d
What about running the following? Do you still get an error?
Code:
$ xdate=2009-03-27
$ date -d "$xdate 10 days" +%m-%d
 
Old 04-08-2009, 04:35 PM   #6
ne00
LQ Newbie
 
Registered: Apr 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you so much. Works great!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
Deleting files after x days from the latest date file sriramsreedhars Programming 8 03-03-2007 09:30 AM
Passed CNE two days ago sundri Linux - Certification 1 10-17-2006 12:03 PM
Invalid UTF8 string passed to pango_layout_ Curtux Mandriva 0 05-24-2005 07:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration