Solved

Using DateJS to add months, function not working

Posted on 2011-09-03
6
606 Views
Last Modified: 2012-05-12
My screen: https://www.interactivehealthpartner.com/mfc_goals.asp
DateJS documention: http://code.google.com/p/datejs/wiki/APIDocumentation

Code attached. This line not working:
var weight2aa=date.weight2a.addMonths($('#term').val());

From the documentation:

addMonths
.addMonths ( Number months ) : Date
Adds the specified number of months to this instance given the number of months to add. The number can be positive or negative.
Parameters
{Number months} The number of months to add. The number can be positive or negative. required
Return Value
{Date} this
Example
Date.today().addMonths(6);
Date.today().addMonths(-6);

Firebug says: "date is not defined". I must be doing something wrong, would appreciate any advise. Thank you!
$('#weight1d').click(function(){
	if ($('#weight1a').val().length>0 && $('#weight1b').val().length>0){
		if (isNumber($('#weight1a').val()) && isNumber($('#weight1b').val())){
			$('#weight1a').removeClass('redbg');
			$('#weight1b').removeClass('redbg');
			var weight2a=$('#startdate option:selected').val();
			var weight2aa=date.weight2a.addMonths($('#term').val());
			alert(weight2aa);
		} else {
			if (not(isNumber($('#weight1a').val()))){$('#weight1a').addClass('redbg');}
			if (not(isNumber($('#weight1b').val()))){$('#weight1b').addClass('redbg');}
		}
	} else {
		if ($('#weight1a').val().length==0){$('#weight1a').addClass('redbg');}
		if ($('#weight1b').val().length==0){$('#weight1b').addClass('redbg');}
	}
});

Open in new window

0
Comment
Question by:bbdesign
  • 2
  • 2
  • 2
6 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 36479481
What version of Firefox are you using?  your "date" variable is lowercase, try Date
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 36480022
This looks so wrong in so many ways

var weight2a=$('#startdate option:selected').val();// a string, not a date

var weight2aa=date.weight2a.addMonths($('#term').val()); // does not make any sense.

Perhaps you mean
http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

// assuming term contains a number and weight2a contains mm/dd/yyyy
var weight2aa=Date.parseExact(weight2a,"M/d/yyyy").addMonths($('#term').val()).toString("M/d/yyyy");
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 36480276
mplungian is right about
var weight2aa=date.weight2a.addMonths($('#term').val());

weight2a is not a Date object and val() returns a string

so is should read (using parseInt - FYI you will get NaN if it's not a number):

var weight2aa = Date.addMonths(parseInt($('#term').val()));

though I disagree with the addMonths as it accepts a number not a string or date:

.addMonths ( Number months ) : Date
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:bbdesign
ID: 36480415
Thanks. This helps, but I still get an error. I made sure my variables are Date and Integer now, using the attached code. When I run it in Firebug, it says: Date.weight2aDate is undefined (line 3). Any advice given that? Thank you.
var weight2aDate=Date.parseExact($('#startdate option:selected').val(), 'M/d/yyyy');
var weight2aTerm=parseInt($('#term').val());
var weight2aEnd=Date.weight2aDate.addMonths(weight2aTerm);
alert(weight2aDate);
alert(weight2aTerm);
alert(weight2aEnd);

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36480447
Try weight2aDate.addMonths instead of Date....
I'll look later
0
 

Author Comment

by:bbdesign
ID: 36480459
I believe that did it! Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now