Solved

Using DateJS to add months, function not working

Posted on 2011-09-03
6
608 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

803 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