3

am trying to get the date and modify on it here is the code

           <script type="text/javascript">
           $(function() { 
            $("#terms_of_payment").change(function(){ 
                 var element = $(this).find('option:selected'); 
                 var days = element.attr("value"); 
                 var start = $('input[name="date"]').val();
                 var date = new Date(start);
                      var day = date.getDate() + days;
                      var month = date.getMonth();
                      var year = date.getFullYear();
                var edate = day + "-" + month +  "-" + year;
               alert(start);
               alert(edate);
               $('#due_date').val(edate); 
              }); 
              });
              </script>

it gives the right start date , but when trying to add to it ,it just gives me the wrong month and day ,,,,, plz help

here is the html

            <div class="form-item">
            <label>
                <span class="required">*</span>
               date
            </label>

              <input type="text" readonly="readonly" value="" id="date" name="date" class="hasDatepicker">            
          </div>
           <div class="form-item">
            <label>
              due date
            </label>  
                 <input type="text" disabled="disabled" value="" id="due_date" name="due_date">               </div>
               <div class="form-item">
              <label>
                <span class="required">*</span>
                terms-of-oayment
                </label>

                <select class="" id="terms_of_payment" name="terms_of_payment">
                <option selected="selected" label="---------" value="">---------     
                </option>
                <option label="مستحقة الدفع لدى تسلمها" value="1">مستحقة الدفع لدى تسلمها</option>
                 <option label="مستحقة الدفع في غضون 15 يوما" value="15">مستحقة الدفع في غضون 15 يوما</option>
                 <option label="مستحقة الدفع في غضون 30 يوما" value="30">مستحقة الدفع في غضون 30 يوما</option>
                  <option label="مستحقة الدفع في غضون 45 يوما" value="45">مستحقة الدفع في غضون 45 يوما</option>
                   <option label="مستحقة الدفع في غضون 60 يوما" value="60">مستحقة الدفع في غضون 60 يوما</option>
                   <option label="مستحقة الدفع في غضون 90 يوما" value="90">مستحقة الدفع في غضون 90 يوما</option>
              </select>
                   </div>
  • the date starts right, but then doesn't work out later? – necromancer May 05 '12 at 18:07
  • am using the alert to know what goes on wrong ,,, the first alert to know the date that i took , the second one (with the problem) to know the date that am going to insert ... it just gives me " 515-0-2012" the first field is for day but in combine the month with the day :S – mahmoud abu-seini May 05 '12 at 18:08
  • Post your relevant HTML please. – Marc May 05 '12 at 18:09
  • Note that in JavaScript, `.getMonth()` returns the index of the month (i.e. January = 0; December = 11) – paulslater19 May 05 '12 at 18:11

2 Answers2

3

Try generating the new date like so:

var edate = new Date(date.getTime() + days *24*60*60*1000);

See also Add days to JavaScript Date.

Community
  • 1
  • 1
Conan
  • 2,659
  • 17
  • 24
3

Two problems here:

  1. .getMonth() returns 0 to 11, not 1 to 12. See http://www.w3schools.com/jsref/jsref_getmonth.asp

  2. For example, presume the date is 1/31/2012 and you're adding one day. You'd yield 1/32/2012.

In theory, you'd have more success casting to something like Julian date then adding the days. Or look to http://www.datejs.com/ as a great library for doing date math, formatting, and parsing in JavaScript.

robrich
  • 13,017
  • 7
  • 36
  • 63