I am experimenting with dates and i found out the values from a date type input
are strings. I want to convert the number parts of the string that is returned, to numbers.
I suppose the best way to describe what i am trying to do is by pasting in the code.
here is the relevant HTML code
<input id="dateinput" type="date"> </input>
<input type="button" onclick="calcdiff()" value="submit"> </input>
Here is my javascript code. the ctc element is where i test to see if i get the correct output.
function calcdiff() {
var datetest = document.getElementById("dateinput").value;
var yearString = datetest.slice(0,4);
var monthString = datetest.slice(5,7);
var dayString = datetest.slice(8,10);
var yearNum = Number("yearString");
var monthNum = Number("monthString");
var dayNum = Number("dayString");
document.getElementById("ctc").innerHTML = yearNum;
What I have tried:
when i use typeof yearNum, i can see it is converted to a number type. However,
when i just set the innerHTML to yearNum as in the code above, it returns "NaN".
That is even though if i set the ctc element to display yearString, it displays "2018", if that is the year i choose from the date input element.
What is wrong with the code since it returns "NaN" and not the number?