简体   繁体   中英

How can I add or subtract days to inputs?

I'm trying to add or subtract some days to inputs that I have. In fact it comes from my registered data from mysql. It give me some days and I would like to add or subtract.

Actually I've done the function for adding days.

Here comes the function::

​<script type="text/javascript">
    function addday() {
        var items = new Array();
        var itemCount = document.getElementsByClassName("date");

        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
        }



        for (var i = 0; i < itemCount.length; i++) {
            items[i] = document.getElementById("date" + (i + 1)).value;
            var itemDtParts = items[i].split("-");
            var itemDt = new Date(itemDtParts[2], itemDtParts[1] - 1, itemDtParts[0]);
            nb=document.getElementById('nb')

                itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+nb)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();





        }
       return items;
       }
</script>

the real concern is that instead of giving me the day plus the number of day that I wrote in the input field for that.

It displays to me something like that:

27[object HTMLInputElement]-08-2012

So I do not know how to change it.

Receive all my utmost respect.

thank you very much for your help.

SP.

Change:

nb=document.getElementById('nb')

To:

nb=document.getElementById('nb').value;

and you should be good

nb=document.getElementById('nb')

itemCount[i].value = (itemDt.getDate() < 9 ? "0" : "")+ (itemDt.getDate()+nb)+ "-" + (itemDt.getMonth() < 9 ? "0" : "") + (itemDt.getMonth() + 1) + "-" + itemDt.getFullYear();

itemCount[i].value seems to be strange. I suppose it should be items[i]

Also I think the first if statement duplicates the second one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM