Solved

Using DateJS to add months, function not working

Posted on 2011-09-03
6
598 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now