nextDay, nextWeek, nextMonth Javascript changing date shown in textfield

253 views Asked by At

Im pretty sure there is an answer somewhere, found many of them, but somehow couldnt implement it in my code. I'm aware of mistakes in my code, however Im not sure how javascript works in that case, therefore there is my question. I'd like to know the reason why my function does not work correctly (why months are added?) and how to make it work. Thanks for any kind of answer.

<input type='text' id='calendar_form' />
<input type='button' onclick="nextDay()" value='NextDAY' />
<script>
    function nextDay() {
        var example = document.getElementById('calendar_form').value;
        var date = new Date();
        var parts = example.split('.');
        date.setFullYear(parts[2], parts[0], parts[1]);
        date.setTime(date.getTime() + 86400000);
        var dateDay = date.getDate();

        var dateMonth = date.getMonth();

        var dateYear = date.getFullYear();

        document.getElementById("calendar_form").value = dateDay + "." + dateMonth + "." + dateYear;

    }
</script>

What Im trying to achive is a set of functions which allow me to change date (dd.MM.yyyy) shown in textfield (as next/prev day, next/prev week, next/prev month). If you can point me in any good direction to look for an answer, Id be grateful.

1

There are 1 answers

2
pimvdb On

You swapped month/year because the order is year-month-date. I'd recommend new Date rather than setFullYear as well. You could do: http://jsfiddle.net/nQtQP/.

var date = new Date(parts[2], parts[1], parts[0]);