In Javascript, I am having difficulty adding or subtracting 1 day from a date in a form text field

I have already tried rewriting this a few different ways and each time I ran into isssues. The code attached is the best I came up with so fat, but it has a problem that I hope someone can help me with.
I have a form on the screen with an input text field having id and name "'ShowThisDate'". That field will contain a date in the format M/D/YYYY such that M is based off 1=Jan...12=Dec This is what I need to do in Javascript.:
1. obtain the date from the 'ShowThisDate' text input form field.
2. Based on if a -1 or 1 is passed to the function, I need to either subtract 1 day or add 1 day to the date obtained in step 1.
3. format the new date as part of a url string using the format M/D/YYYY such that M is again based off 1=Jan...12=Dec

The example code works, and does not throw any errors, however it behaives strangly and I am pretty sure it has something to do with the fact that JavaScript uses a base 0=Jan, 1=Feb...11=Dec for the month.
If I started off with a date of 11/11/2008 in the 'ShowThisDate' field and I clicked on the back arrow with calls the function PicChange('-1'), it correctly created the url with d=11/10/2008. That url does an AJAX call to get the next picture and caption in the sequence, which also sets the 'ShowThisDate' field with the just aquired date. So in this case the date in that box would update to 11/10/2008. So one day's cycle is complete and worked flawlessly.

If I click the back button again it continues to display the corrrect dates of 11/9/2008... 11/8/2008.... 11/7/2008.... etc. Once it gets to 11/1/2008, it does display 10/31/2008 next, but the problem is when you go back another day, rather than it building the url with d=10/30/2008, it uses 9/30/2008 and then 9/29/2008... What happened to Oct 1 through Oct 30? So here is the pattern I get when starting with Nov 1st 2008 and moving backwards (-1):
11/1/2008, 10/31/2008, 9/30/2008, 9/29/2008...
...9/1/2008, 8/31/2008, 7/31/2008, 6/30/2008, 6/29/2008...
...6/1/2008, 5/31/2008, 4/30/2008, 4/29/2008...

Can someone please help be figure out what the problems is? I would be most greatful. If necessary, I can move the whole code to my public accessible server so you can see the problem first hand. Oh, and I am using IE 7, but it needs to work in all browsers. -Thanks!!
function PicChange(ThisDir) {
	theChange=parseInt(ThisDir); if (theChange == "NaN") {theChange=0;}
         tDate='11/11/2008'
	FragDate=document.getElementById('ShowThisDate').value.split("/");
	var myDate = new Date;
	myDate.setDate(FragDate[1]);
	myDate.setMonth(parseInt(FragDate[0])-1);
	myDate.setFullYear(FragDate[2]);
	myDate.setDate(myDate.getDate()+(1*theChange));
	xx=parseInt(myDate.getMonth())+1 + "/" + myDate.getDate() + "/" + myDate.getFullYear();
	if (Date.parse(xx)>=Date.parse(tDate)) {
		alert("You'll have to wait until midnight (Eastern timezone) to see tomorrow's picture...");
	} else if (Date.parse(xx) < Date.parse('11/11/2004')) {
		alert("Sorry, We don't have any pictures of the day prior to 11/11/2004");
	} else { 
	url = 'liveaction.cfm?a=950229822&d='+xx; alert(url);
	postRequest(url);
	}
}

Open in new window

bobPUNKbobAsked:
Who is Participating?
 
SreejithGCommented:
The following function does what you are looking for
function func(duration)
{
date1 = document.getElementById('ShowThisDate').value;
d1=date1.split("/");
var myDate=new Date(d1[2],d1[0]-1,d1[1]);
myDate.setDate(myDate.getDate()+parseInt(duration));
var dd = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
newDate =(dd.getMonth()+1) + "/" + dd.getDate() + "/" + dd.getFullYear();
// newDate contains the date you are looking
 
}

Open in new window

0
 
bobPUNKbobAuthor Commented:
Thank you so much! Awesome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.