var today = new Date()
var time = today.getFullYear() +'-'+ today.getMonth()+1 +'-'+ today.getDate();
document.getElementById("date").innerHTML = time;
<p1 id = "date" />
I've used this code for another task on this same HTML page and it worked
var today = new Date()
var time = today.getFullYear() +'-'+ today.getMonth()+1 +'-'+ today.getDate();
document.getElementById("date").innerHTML = time;
<p1 id = "date" />
I've used this code for another task on this same HTML page and it worked
You have error in your syntax. Remove the '
at the end of your js.
Also you can use today.toISOString()
to format the date as 'Y-m-d'.
var today = new Date();
document.getElementById("date").innerHTML = today.toISOString();
Also, p1
doeas not exist. You surely mean <p>
or in your code: <p id="date"></p>
This code works for me.
<html><head></head>
<body>
<p1 id = "date" />
<script>
var today = new Date();
var time = today.getFullYear() + "-"+ today.getMonth()+1+ today.getDate();
document.getElementById("date").innerHTML = time;
</script>
</body></html>
The p1 tag needs to exist before the script block is called, or the "date" element will be null.
Here is some code that can help with debugging.
<html><body>
<p id = "date1" />
<script>
var el_1 = document.getElementById("date1");
var el_2 = document.getElementById("date2");
alert("el_1=" + el_1 + "\n" + "el_2=" + el_2);
</script>
<p id="date2" />
</body></html>
When I run the second code, I get el_2=null.