Solved

Using DateJS to add months, function not working

Posted on 2011-09-03
6
612 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
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Rob
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 43

Expert Comment

by:Rob
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 40
Video Tutorial help 2 38
Use Mid in Html 6 22
Table header must be on top 2 27
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

756 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