I want to put the current date and time in a span The HTML code is below snippet:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="Exe3.js"></script> <title>HomePage</title>
<h1>Current Date is :: <span id="dateNow">XX</span></h1>
<h1>Current Time is :: <span id="timeNow">XX</span></h1>
My JS File is:
function showDate(){
let currDateTime = new Date();
let currDate = currDateTime.toLocaleDateString();
console.log(currDate);
document.getElementById("dateNow").innerHTML = currDate;
}
function showTime(){ let currDateTime = new Date(); let hour = currDate.getHours(); let minute = currDate.getMinutes(); let second = currDate.getSeconds(); let timeString = hour+" : "+minute+" : "+second; console.log(timeString); document.getElementById("timeNow").innerHTML = timeString; }
showDate(); showTime();
I am getting the below error
Uncaught Type Error: Cannot set properties of null (setting 'innerHTML')
I am new to learning JavaScript