Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to do DateAdd in JavaScript - help

Status
Not open for further replies.

Cathy105

Programmer
Jul 7, 2002
12
US
I've only been using JavaScript for 2 days (extreme newbie). Vb has a dateAdd function that I need to emulate in Javascript.
Below is what I have tried, if anyone can point out my errors that would be great. (Got the dateadd function from the net). I know this is alot to ask but I'm pretty lost.

Appreciate any help anyone can offer.

<SCRIPT language=&quot;JavaScript&quot; >
<!--

function AddYear(ddate,flag,numyears){
var s = &quot;&quot;;
var newDate;
if(flag){ // i.e. dd/mm/yyyy format
var dstr = ddate.split(&quot;/&quot;);
newDate = dstr[1]+&quot;/&quot;+dstr[0]+&quot;/&quot;+dstr[2];
}
else{newDate = ddate}; // mm/dd/yyy format
d = new Date(newDate);
d.setFullYear(numyears+d.getFullYear());
if(flag){
s += (d.getDate()<10?&quot;0&quot;+d.getDate():d.getDate()) + &quot;/&quot;;
s += ((d.getMonth() + 1)<10?&quot;0&quot;+(d.getMonth() + 1):(d.getMonth() + 1)) + &quot;/&quot;;
}
else{
s += ((d.getMonth() + 1)<10?&quot;0&quot;+(d.getMonth() + 1):(d.getMonth() + 1)) + &quot;/&quot;;
s += (d.getDate()<10?&quot;0&quot;+d.getDate():d.getDate()) + &quot;/&quot;;
}
s += d.getYear();
return(s);
}

function check_it()
{
var complete=true;
var casename=document.frmCaseNew.txtCasename.value;
var accrual=document.frmCaseNew.txtAccrualDate.value;
var expire=document.frmCaseNew.txtExpiration.value;
var duration=document.frmCaseNew.textYears.value;
if ((casename==&quot;&quot; )|| (casename==null))
{
alert(&quot;Case Name is Required&quot;);
complete=false;
return complete;
}

if ((accrual==&quot;&quot; )|| (accrual==null))
{
alert(&quot;Accural Date is Required&quot;);
complete=false;
return complete;
}

if ((expire==&quot;&quot; )|| (expire==null))
{
alert(&quot;Expiration Date is Required&quot;);
complete=false;
return complete;
}

if ((duration==&quot;&quot; )|| (duration==null))
{
alert(&quot;Number of Years is Required&quot;);
complete=false;
return complete;
}
if ((accrual!=&quot;&quot; )|| (accrual!= null)) & ((expire!=&quot;&quot; )|| (expire!=null)) & ((duration!=&quot;&quot; )|| (duration!=null))

{
function AddYear(ddate,flag,numyears);
return (s);
var dteNew =( +AddYear(accrual,false,1);
}
If (expire != dteNew)
{
alert(&quot;The Statute Expiration Date Does Not Equal the Amount of Years permitted for Statute Limitation Deadline on this case! Please check your dates.&quot;);
complete=false;
return complete;
}
}

//-->
</Script>
 
looks very complicated. You are just trying to add years to a date right?

From my answer in thread216-631417:
Code:
var curDate = new Date(); //today's date
curDate.setFullYear(curDate.getFullYear + 20); //+20 years

Check out the ECMAscript reference for fairly standardised javascript reference :

Also the FAQs tab for this forum should give you a goodly amount of info, as will the keyword search tab :)

good luck


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin - thanks for responding so quickly.

I need to pull the dates from the form after the user enters them, could I set a new Date() property to what they
enter?

Also, what about Leap years?

Thanks!
 
it's all in the ECMA javascript reference. Specifically: for the Date object.

Looks like you can create a new date with a string.
Code:
var curDate = new Date(&quot;20 Aug 2003&quot;);

Try getting JUST the date to validate, then add all your other stuff later.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I tried getting the input validation to work first. Which is fine. I have been to devguru and I purchased a PDF of the Javascript info.

The addDate function alone works fine also. My problem
is getting the two functions to work together on_submit.

I tried adding a verify function that called the AddDate function and then tried having the check_it function call the verify() function, but no luck. Or..I'm not calling it correctly. I keep getting object required error.
 
well looking at your validation code you have some syntax errors in the last bit with the function. You don't need a function, just the two lines of code I supplied in my first post.
Try something like:
Code:
//this if statement can probably be simplified to just
// if (accrual != &quot;&quot;)
if ((accrual!=&quot;&quot; )|| (accrual!= null)) & ((expire!=&quot;&quot; )|| (expire!=null)) & ((duration!=&quot;&quot; )|| (duration!=null))
{ 
  //make a Date object from string variable accrual
  var accrualDate = new Date(accrual); //string must be in correct date format

  //make a Date representing 20 years from now
  var compDate = new Date(); //today's date
  compDate.setFullYear(compDate.getFullYear + 20); //+20 years

  //not sure what you actually need to compare
  //lets make it fail if accrualDate is over 20 yrs away
  if accrualDate > compDate {
    alert(&quot;accrual is over 20 years away. Fail.&quot;);
    return false;
  }
}

see if you can adapt that

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top