0

Trying to add a number of days to an initial date when selected by a user.

So an Admin creates settings - the setting im trying to use is a set number of days to be added. The user selects a date on a form and this number of days is added.

My form code:

<div class="col-xl-4">
<p class="card-text">Invoice Date: *
<input id="pay_term" type="hidden" name="pay_term" value="<? echo str_replace("'","",INVOICE_PAYMENT_TERM)?>"></p>
<input id="invoice_date" type="date" name="invoice_date" onchange="getdate()" required></p>
</div>

so INVOICE_PAYMENT_TERM holds the settings for number of days in a php file as define('INVOICE_PAYMENT_TERM', '15'); // Invoice Payment Term (Days)

This shows fine - for example - the number 15

Then the javascript -

<script>
        $(document).ready(function () {
        $('#invoice_date').datepicker();
    });
    function getdate() {
        var tt = document.getElementById('invoice_date').value;
        var nod = document.getElementById('pay_term').value;

        var date = new Date(tt);
        var newdate = new Date(date);

        newdate.setDate(newdate.getDate() + nod);

        var dd = ('0' + newdate.getDate()).slice(-2);
        var mm = ('0' + (newdate.getMonth()+1)).slice(-2);
        var y = newdate.getFullYear();

        var someFormattedDate = dd + '/' + mm + '/' + y;
        document.getElementById('invoice_due_date').value = someFormattedDate;
    }
    </script>

if INVOICE_PAYMENT_TERM is set at 15 and i select the invoice date of 01/03/2023 - I would expect 16/03/2023 but instead get 23/06/2023

newdate.setDate(newdate.getDate() + nod); If i change this line to newdate.setDate(newdate.getDate() + 15); it works fine and i get the expected date - it seems to not work after i set '15' as the variable 'nod' (numberofdays)

Thankyou in advance.

1 Answers1

-1

var nod = document.getElementById('pay_term').value; returns the number of days as a string, so you have to convert it to an integer before adding it to newdate.getDate(), otherwise you will get unexpected results.

Change newdate.setDate(newdate.getDate() + nod); to newdate.setDate(newdate.getDate() + parseInt(nod));.

Pavel Savva
  • 449
  • 2
  • 7
  • This does not add value to the answers already given in https://stackoverflow.com/questions/563406/how-to-add-days-to-date – Tibrogargan Mar 21 '23 at 22:33
  • @Tibrogargan which answer in your link are you referring to? The top several answers would not fix the issue in this question - they would also provide incorrect results if the number of days provided is a string and not an integer. So I believe my answer actually fixes the issue in this question unlike the link you provided... Could you please point me to the answer in your link that would fix the issue in this question? – Pavel Savva Mar 21 '23 at 22:39
  • Both the answers that use `parseInt`. But further, there are dozens of questions concerning the behavior of the `+` operator when given mixed type arguments that this question is really a duplicate of that your answer also doesn't add value to. – Tibrogargan Mar 21 '23 at 23:34
  • I must be missing something here... Could you please point me to the answer with `parseInt` you are referring to? I have looked through the top 5-10 answers in the question you linked and none of them use `parseInt`. I believe my answer provides a working solution to Simon Hammond's question. It's meant to do just that - answer his question, not send him down the rabbit hole of related concepts that *might* solve his issue. I believe this is what StackOverflow is all about at the end of the day. – Pavel Savva Mar 21 '23 at 23:43
  • 1
    Well doing exactly this fixed my problem - So thankyou for the answer Pavel – Simon Hammond Mar 22 '23 at 20:57
  • @PavelSavva It's great that you were able to help the OP out. The reason for the behavior in the question is actually that the JavaScript addition operator coerces the non-string operand to a string if any operand is a string ([reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition)). Your answer corrects the problem, but neither the question nor your answer add value to the StackOverflow site, since similar questions have been asked before and the answer just provides a literal fix which is unlikely to be useful to anyone but the OP. – Tibrogargan Mar 23 '23 at 05:01
  • @Tibrogargan good point, feel free to write a better answer! – Pavel Savva Mar 23 '23 at 05:45