?
Solved

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

Posted on 2008-11-11
2
Medium Priority
?
528 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:bobPUNKbob
2 Comments
 
LVL 3

Accepted Solution

by:
SreejithG earned 2000 total points
ID: 22929005
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
 

Author Closing Comment

by:bobPUNKbob
ID: 31515446
Thank you so much! Awesome!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question