0

I have 2 input typ button and an input type textfield. The both button are there to activate a function for adding or substracting some days .

In fact all is generated by ma database from mysql.

the thing is that I have many dates because the php script does manage some calendars with some actions associated to the dates.

sometimes we needs to manage the calendar in order to give few days after the meetings.

So i've done that function:

in javascript:

​<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').value;


                itemCount[i].value = setDate(itemDt + nb) ;



        }


       return items;
           }

</script>

the concern is that this script does not work.

I can not correct the mistakes I'm trying since 2 hours and i've read many about dates But I don't know how to.

first of all it says to me that setDate is not defined.

And the page is freezing.

Receive all my Utmost Respect.

SP.

2 Answers 2

1

You have to call setDate method on javascript Date object. Use like this

<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').value;

    var newDate = itemDt.getDate() + nb;
        itemDt.setDate(newDate ) ;
                itemCount[i].value = itemDt;

        }


       return items;
           }

</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Dear Sir thanks for your reply but now instead or returning dates it returns me something weird like that 1577833200000
It still returns me the same 1577833200000
Dear sir thanks for your reply now it returns me something like that Wed Jan 01 2020 00:00:00 GMT+0100 (Paris, Madrid) Is it possible to have just dd-mm-YYYY ?
I've tried like that : itemDt.setDate(newDate ).format("dd-mm-yyyy") ; itemCount[i].value = itemDt; but now it says to me Uncaught TypeError: Object 1577833200000 has no method 'format'
Uncaught TypeError: Object Sun Apr 28 2013 00:00:00 GMT+0200 (Paris, Madrid (heure d’été)) has no method 'format'
1

http://jsfiddle.net/eeG5u/

var today = new Date();
var yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 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.