Solution 1 :

change this line:

$('#retrieveDate').text($.datepicker.formatDate('yy-dd-mm', myDate));

by this: to add text by jQuery you should use val()

$('#retrieveDate').val($.datepicker.formatDate('yy-dd-mm', myDate));

Solution 2 :

Maybe the problem is your declaration of ‘myDate’ as var and then trying to write it as a text. Try converting myDate to a String after you add 15 days.

Problem :

I picked this code from jQuery forum.

$(function () {
    $("#selectedDate").datepicker({
        dateFormat: 'yy-mm-dd',
        onSelect: function(_date) {
            var myDate = $(this).datepicker('getDate');
            console.log(myDate);
            myDate.setDate(myDate.getDate() + 15);
            $('#retrieveDate').text($.datepicker.formatDate('yy-dd-mm', myDate));
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div class="form-group">
    <label>Selected Date in Datepicker</label>
    <input type="text" name="selected_date" class="form-control datepicker"
           id="selectedDate">
</div>
<div class="form-group">
    <label>Retrieve Date</label>
    <input type="text" id="retrieveDate" name="retrieve_date"
           class="form-control">
</div>

When I select the date in first input, the second input must be automatically filled with the date that is 15 days more from the selected date in the datepicker.

Comments

Comment posted by Roland Starke

Inputs usually have values, can you try changing

Comment posted by Rick Bronger

just use .val(..) instead of .text(…) .val is used for inputfields. text is used for non inputfields

Comment posted by jQuery UI’s Datepicker component documentation shows it has a

jQuery UI’s Datepicker component documentation shows it has a setDate function for this purpose

Comment posted by sd077

Thanks It worked. I had tried using .val() but don’t why it didn’t work and now it is working. Thank you all.

Comment posted by Roland Starke

It looks like there are 2 date formats

By

Leave a Reply

Your email address will not be published. Required fields are marked *