Advertisement

07.09.2007 at 05:17PM PDT, ID: 22684821
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

Need to add a variable number of months to a start date and calculate an accurate end date

Asked by bp360 in Scripting Languages, JavaScript

Tags: , , , ,

So I have a number of months and 3 date fields (for both start and end) - I have a function in progress that kinda works (from a developer/js hack, not me) which is supposed to add the number of months (term) to the start date to set the 'end date' to the correct date... but its using math to create dates instead of a true calendar so if I put in 12/31/2000 with a term of 12 months (include the start month as one so +11 mos) it returns 11/31/2001 - which doesn't exist (Nov. has only 30 days) - so I'm open to a complete rewrite - but it needs to behave properly and return a real date in the 'end date' - requirements are to run the calculation on change of any of the four fields (term, start_date_month, start_date_day, start_date_year) - if any of these four fields are empty the function should not run, if a non number is entered an alert should pop up saying "enter a valid number" and when we finally run our date addition - we should return a real date and pop it into the 3 end date fields.

Heres the current code - again - i'm open to a 'fix' or a rewrite so long as it works and covers at least what the following does:


<script type="text/javascript">
      
function calcTermDate () {
      
      var numOfMonths = document.getElementById('schednoteTerm').value;       
      var month = document.getElementById('schednoteTermCommenceDateMonth').value;
      var day            = document.getElementById('schednoteTermCommenceDateDay').value;
      var year       = document.getElementById('schednoteTermCommenceDateYear').value;
      
      if (year >= 1999 && year <= 2025) {      

       if ((month && month <= 12) && (day && day <= 31) && year && numOfMonths) {                  

            //Joining three date fields to create a commencement date
            var newDate = new Date(month + "/" + day + "/" + year);
            var d                   = new Date(newDate);                               
            var day             = d.getDate();
            
            //Adding term to commencement date
            d.setMonth(d.getMonth() + parseInt(numOfMonths));             
            if (d.getDate() < day) {
              d.setDate(1);
              d.setDate(d.getDate() - 1); //End of Term date
            }            
            
            // Spliting into three date values
            var newDay                   = d.getDate();
            var newMonth             = d.getMonth();
            var newYear             = d.getFullYear();      
       }
      }
            
      // Error check
      if (isNaN(month) || isNaN(day) || isNaN(year)) {
            alert ("Invalid Date");
      }                   
      
      // Populating End of Term date values if created
      if (newDay && newMonth && newYear) {      
            if (document.getElementById('schednoteTermCommenceDateMonth')) {
                  if (newMonth < 10) { newMonth = '0'+newMonth}
                  document.getElementById('schednoteTermEndDateMonth').value = newMonth;
                  document.getElementById('endMonth').value = newMonth;
            }
            if (document.getElementById('schednoteTermCommenceDateDay')) {
                  if (newDay < 10) { newDay = '0'+newDay}
                  document.getElementById('schednoteTermEndDateDay').value = newDay;
                  document.getElementById('endDay').value = newDay;
            }
            if (document.getElementById('schednoteTermCommenceDateYear')) {
                  if (newYear < 10) { newYear = '0'+newYear}
                  document.getElementById('schednoteTermEndDateYear').value = newYear;
                  document.getElementById('endYear').value = newYear;
            }
      } else {
                  document.getElementById('schednoteTermEndDateMonth').value = '';
                  document.getElementById('endMonth').value = '';
                  document.getElementById('schednoteTermEndDateDay').value        = '';
                  document.getElementById('endDay').value        = '';
                  document.getElementById('schednoteTermEndDateYear').value       = '';
                  document.getElementById('endYear').value       = '';
      }
}// End of calcTermDate
</script>


                     <div>Term: (Months)<br><input  type="text" id="schednoteTerm" name="schednoteTerm" value="" onChange="" onBlur="calcTermDate();"></div>
                        <div>Start Date:<br>
                              <input  type="text" id="schednoteTermCommenceDateMonth" name="schednoteTermCommenceDateMonth" maxlength="2" value="" onChange="calcTermDate();"> /
                              <input type="text" id="schednoteTermCommenceDateDay" name="schednoteTermCommenceDateDay" maxlength="2" value="" onChange="calcTermDate();"> /
                              <input type="text" id="schednoteTermCommenceDateYear" name="schednoteTermCommenceDateYear" maxlength="4" value="" onChange="calcTermDate();">
                        </div>
                        <div>End Date:<br>
                              <input type="text" id="schednoteTermEndDateMonth" name="schednoteTermEndDateMonth" maxlength="2" value=""> /
                              <input type="text" id="schednoteTermEndDateDay" name="schednoteTermEndDateDay" maxlength="2" value=""> /
                              <input type="text" id="schednoteTermEndDateYear" name="schednoteTermEndDateYear" maxlength="4" value="">
                        </div>
Start Free Trial
[+][-]07.09.2007 at 05:57PM PDT, ID: 19450027

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Scripting Languages, JavaScript
Tags: add, date, days, variable, month
Sign Up Now!
Solution Provided By: amit_g
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.09.2007 at 11:13PM PDT, ID: 19451006

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.26.2008 at 04:11PM PST, ID: 20990275

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 04:36AM PST, ID: 21021299

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 08:33AM PST, ID: 21022262

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 09:26AM PST, ID: 21022547

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 11:16AM PST, ID: 21023021

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32