Using DateJS to add months, function not working

My screen:
DateJS documention:

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

From the documentation:

.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.
{Number months} The number of months to add. The number can be positive or negative. required
Return Value
{Date} this

Firebug says: "date is not defined". I must be doing something wrong, would appreciate any advise. Thank you!
	if ($('#weight1a').val().length>0 && $('#weight1b').val().length>0){
		if (isNumber($('#weight1a').val()) && isNumber($('#weight1b').val())){
			var weight2a=$('#startdate option:selected').val();
			var weight2aa=date.weight2a.addMonths($('#term').val());
		} 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

Brad BansnerWeb DeveloperAsked:
Who is Participating?
Michel PlungjanIT ExpertCommented:
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

// 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");
RobOwner (Aidellio)Commented:
What version of Firefox are you using?  your "date" variable is lowercase, try Date
RobOwner (Aidellio)Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Brad BansnerWeb DeveloperAuthor Commented:
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);

Open in new window

Michel PlungjanIT ExpertCommented:
Try weight2aDate.addMonths instead of Date....
I'll look later
Brad BansnerWeb DeveloperAuthor Commented:
I believe that did it! Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.