简体   繁体   中英

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"

Convert the string to a Date object:

new Date(input).getYear()

(input being the value of your date input)

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