0

I'm trying to fetch javascript variavle value to text field, but I'm getting an error.

[object Object]

enter image description here

I'm trying to calculate difference between two dates and want to show that in text field and after that want to save it in database.

When I'm displaying the result in span tab and it gives me proper result. enter image description here

Here is my code: View file

<div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11"> 
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type='text' class="form-control" id="datepicker1" id="yes" name="property_gas_issue_date" placeholder="Issue Date " />
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" id="datepicker8" name="property_gas_expiry_date" placeholder="Gas expiry date"/>
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <!-- <input type="text" id="diff" name="property_gas_certificate_duration_days" placeholder="duration in days" value="" class="form-control"> -->
                        <strong><span> Renewal days count:</span> <span id='diff'> - </span> <span> Days</span></strong>
                    </div>
                </div>
            </div>

Note: In above code I just comment textbox code.

JS code:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
  });
</script>

This JS code for textbox field:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      var result = $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);

      document.getElementById("diff").value = result;
  });
</script>

Any kind of help is welcome, thanks in advance.

Updated question:

here is my code:

<script>

  var date1 = new Date();
  date1.setMonth(date1.getMonth() + 12);
  $('#datepicker4').datepicker("setDate", new Date());
  $('#datepicker5').datepicker("setDate", date1);

  //$('#datepicker4').datepicker();
  $("#datepicker4").datepicker({ onSelect: function(dateText) { var dateofDatePicker1 = dateText ;
  $('#datepicker5').trigger('change'); } });

  $('#datepicker5').datepicker();
  $('#datepicker5').change(function () {
      var gasDiff= $('#datepicker5').datepicker("getDate") - $('#datepicker4').datepicker("getDate");
      var result = gasDiff / (1000 * 60 * 60 * 24) * -1;

      document.getElementById("gasDiff").value = result;
  });
</script>

output: enter image description here

In above picture the result textbox is empty.

2 Answers 2

1

Update result variable in here, try it

 <script>
    $('#datepicker1').datepicker();
    $('#datepicker8').datepicker();

    $('#datepicker8').change(function () {
        var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
        var result =  diff / (1000 * 60 * 60 * 24) * -1 ;

        document.getElementById("diff").value = result;
    });
</script>    
Sign up to request clarification or add additional context in comments.

7 Comments

Hey thanks, its working. Will you please explain me the code ?
When you do this $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1); you are making calculation and trying to set to text box val and then text box object is assigned to result variable and ten reassigning it to text box. Here you are using "text" to put calculated value which is not correct instead of creating result variable you can just do this $('#diff').val(diff / (1000 * 60 * 60 * 24) * -1);
Is it possible to generate date in second datepicker if we select date from first datepicker. eg. If I'm selecting 22/11/2017 in first datepicker, then 2nd datepicker will show date after one month i.e. 22/12/2017.
stackoverflow.com/questions/9380131/… referred this link. It gives proper result, but then it wont shows calculated result in text field.
|
1

To Add month to date use date.js plugin. Please reference datejs and use below to add month as below and traigger change

       $("#datepicker4").datepicker({
            onSelect: function (dateText) {
                var nextMonth = new Date(dateText).add(1).month().toString('MM/dd/yyyy');
                $('#datepicker5').val(nextMonth);
                $('#datepicker5').trigger('change');
            }
        });
        $('#datepicker5').datepicker();
        $('#datepicker5').change(function () {
              var diff = $('#datepicker4').datepicker("getDate") - $('#datepicker5').datepicker("getDate");
              $('#diff').val(diff / (1000 * 60 * 60 * 24) * -1);
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.