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

Posted on 2008-11-11
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;}



	var myDate = new Date;





	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);




Open in new window

Question by:bobPUNKbob
    LVL 3

    Accepted Solution

    The following function does what you are looking for
    function func(duration)
    date1 = document.getElementById('ShowThisDate').value;
    var myDate=new Date(d1[2],d1[0]-1,d1[1]);
    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


    Author Closing Comment

    Thank you so much! Awesome!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now