Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Date manipulation giving wierd results

Posted on 2007-11-15
7
155 Views
Last Modified: 2011-09-20
Hi,
I'm stumped again...
    For the date entered 18-FEB-11
     alert('Base:'+MM+'-'+DD+'-'+'20'+YY);   shows Base: 2-18-2011
     alert('Before setDate:'+theDate);       shows Before setDate: Fri Mar 18 00:00:00 CDT 2011
     alert('After setDate:'+theDate);        shows After setDate: Sat Mar 19 00:00:00 CDT 2011
     
     How is it incrementing the Month too?
     
     var MM = mon[fV[1].toUpperCase()]+1;   is adding one to the array value of 0-11
     and it is before the increment of the month occurs.
     
     
  } else {
     // See if done entering the date before checking for plus/minus sign
     //   the minus sign is the same as hyphen used in DD-MON-YY
     if(theField.value.length != 9) return true;
     // 2. Check if the key was a plus/minus sign to
     //  increment or decrement
     if(keyCode==187 || keyCode==107) addDay = +1;
     if(keyCode==189 || keyCode==109) addDay = -1;
     if(!addDay) return true;
     // Don't validate unless date is in DD-MON-YY foramt.
     var fV = theField.value.split("-");
     if(fV.length!=3) return true;
     var DD = fV[0];
     var MM = mon[fV[1].toUpperCase()]+1;
     var YY = fV[2];
     alert('Base:'+MM+'-'+DD+'-'+'20'+YY);
     if(!(DD < 32 && MM < 12 && YY.length == 2 )){ return false; }

     var theDate = new Date('20'+YY,MM,DD);
     alert('Before setDate:'+theDate);
     theDate.setDate(theDate.getDate()+addDay);
     alert('After setDate:'+theDate);

     theField.value=formatDateDDMONYY(theDate);
     return false;
0
Comment
Question by:bcarlis
  • 4
  • 2
7 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20293037
bcarlis,

The month is getting messed up the first time you set it.  It is wrong in the Before step.  In Javascript the month count starts at 0 (e.g. Jan = 0).  You will need to subtract 1 from the figure you put in the string to make the date.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20293063
It looks like you could fix it with ...

var MM = mon[fV[1].toUpperCase()];

Let me know if you have a question.

bol
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 20293100
can you post teh full function and the calls you are testing with
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Author Comment

by:bcarlis
ID: 20293709
he whole tamale:

<script language="JavaScript" type="text/javascript">
function dateIncrDecr(theField,e){
var monthname=new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC")
var mon = {JAN:0,FEB:1,MAR:2,APR:3,MAY:4,JUN:5,JUL:6,AUG:7,SEP:8,OCT:9,NOV:10,DEC:11};

  var keyCode = (e.which)?e.which:e.keyCode;
  var addDay = 0;
  function formatDateDDMONYY(pDate){
     var DD = pDate.getDate();
     DD=""+ DD;
     if ((DD.length) < 2) DD = "0" + DD;
     var YY = pDate.getFullYear();
     YY =""+YY;
     return DD+"-"+monthname[pDate.getMonth()]+"-"+YY.substr(2,2);
  }
  // 1. Check if the only value of the field is a plus sign
  if(theField.value=='' && (keyCode==187 || keyCode==107)){
     //alert('Plus alone');
     // If yes, set it to today's date
     var theDate = new Date();
     theField.value=formatDateDDMONYY(theDate);
     return false;

  } else {
     // See if done entering the date before checking for plus/minus sign
     //   the minus sign is the same as hyphen used in DD-MON-YY
     if(theField.value.length != 9) return true;
     // 2. Check if the key was a plus/minus sign to
     //  increment or decrement
     if(keyCode==187 || keyCode==107) addDay = +1;
     if(keyCode==189 || keyCode==109) addDay = -1;
     if(!addDay) return true;
     // Don't validate unless date is in DD-MON-YY foramt.
     var fV = theField.value.split("-");
     if(fV.length!=3) return true;
     var DD = fV[0];
     var MM = mon[fV[1].toUpperCase()]+1;
     var YY = fV[2];
     alert('Base:'+MM+'-'+DD+'-'+'20'+YY);
     if(!(DD < 32 && MM < 12 && YY.length == 2 )){ return false; }

     var theDate = new Date('20'+YY,MM,DD);
     alert('Before setDate:'+theDate);
     theDate.setDate(theDate.getDate()+addDay);
     alert('Afetr setDate:'+theDate);

     theField.value=formatDateDDMONYY(theDate);
     return false;
  }
}
</script>
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 75 total points
ID: 20293897
Did you try my suggestion?  Let me know if you have a question about anything I said.  The problem is occurs before you use setDate() and should be fixed by the correction I suggested.

bol
0
 
LVL 2

Author Comment

by:bcarlis
ID: 20300764
Yes, I see... I had added the one to have it display what it was in the alert BUT it caused it to add to the incremented date.

Simple... Thank you !!

Bill
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20300797
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

808 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