gameloverr2
gameloverr2

Reputation: 95

get month and year from date input

I have the following html code:

<input type="date" id="myDate"/>

<button type="button" class="btn btn-secondary" id="clickMe">MyButton</button>

And I am trying to use the values from the date input (only month and year) in a js function like this:

function myFunction() {
    function function1(a,b)
    {
      //do something with a and b values 
    }

    function function2()
    {
        var a = document.getElementById('myDate').value.month;  //this values are not good
        var b = document.getElementById('myDate').value.year;

        function1(a,b)
    }

    var el = document.getElementById("clickMe");
    if (el.addEventListener)
        el.addEventListener("click", function2, false);
    else if (el.attachEvent)
        el.attachEvent('onclick', function2);
})();

Bassicaly when the button is pressed I want to save the values of the input date as int values (for example 7 and 2020 - month and year) and these values will be transmitted in the js function2 that will call function1 with those parameters.

The problem is that right know my a and b variables are null. Is there any way I can fix this?

I did a debug for those values (a and b) and I get the following error: "TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"

Upvotes: 0

Views: 1577

Answers (1)

user3200120
user3200120

Reputation:

Convert the string to a Date object:

new Date(input).getYear()

(input being the value of your date input)

Upvotes: 2

Related Questions