locked
How to fill with jquery an hidden input with date + 1 month in beacuse of first date input RRS feed

  • Question

  • User1991482975 posted

    Hi,

    i have this date input :

    <input name="PartnerAdvertiseDateActiveStart" class="inputplaceholderviolet wid100x100 valid" id="PartnerAdvertiseDateActiveStartId" aria-invalid="false" aria-describedby="PartnerAdvertiseDateActiveStartId-error" type="date" value="2021-01-07" data-val-required="The Fixez une date de début field is required." data-val="true" data-val-date="Le champ Fixez une date de début doit être une date.">

    on load (by default is the today date) and if i change the date of it  i need to fill this hidden input

    <input name="PartnerAdvertiseDateActiveEnd" id="PartnerAdvertiseDateActiveEnd" type="hidden" value="DATE+1Month">

    with the date of AAA + 1 month

    How to do this script?

    Thanks

    Thursday, January 7, 2021 10:09 AM

Answers

  • User-939850651 posted

    Hi grafic.web,

    If you want to add a month's event to the date as a new date, you can use Date.setMonth() function.

    Something like this:

    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-3.5.1.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#PartnerAdvertiseDateActiveStartId').change(function () {
                    console.log('start date:  ' + $(this).val())
                    var Startdate = new Date($(this).val())
    
                    var EndDate = new Date(Startdate.setMonth((Startdate.getMonth() + 1)))
                    var month = EndDate.getMonth() >= 9 ? EndDate.getMonth() + 1 : ('0' + (EndDate.getMonth() + 1));
                    var day = EndDate.getDate() >= 10 ? EndDate.getDate() : ('0' + EndDate.getDate());
    
                    $('#PartnerAdvertiseDateActiveEnd').val(EndDate.getFullYear() + '-' + month + '-' + day)
                    
                    console.log('end date:  ' +$('#PartnerAdvertiseDateActiveEnd').val())
                })
            })
        </script>
    </head>
    <body>
        <input name="PartnerAdvertiseDateActiveStart" id="PartnerAdvertiseDateActiveStartId" type="date" required>
    
        <input name="PartnerAdvertiseDateActiveEnd" id="PartnerAdvertiseDateActiveEnd" hidden="hidden" type="date">
    </body>

    Result:

    As you can see in the result, it will deal with the boundary issues when the date is modified, for example, there is no 30th in February, and the year is automatically increased.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2021 8:44 AM