Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not getting the date right

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>
like image 430
mahmoud abu-seini Avatar asked May 26 '26 15:05

mahmoud abu-seini


2 Answers

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.

like image 87
Conan Avatar answered May 30 '26 05:05

Conan


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.

like image 38
robrich Avatar answered May 30 '26 05:05

robrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!