Display Current Date and Time Using HTML and JAVASCRIPT

Javascript date object and HTML span element can be used to display the current date and time. By default Javascript use the browser's timezone to display time and date.

JavaScript Date Object

Date object is created with the new Date() constructor. The current date and time is stored inside javascript variable.

Then using innerHTML property, the content of HTML span element is set with current date and time. Unique id of span tag is used by getElementById() method to display the current date and time.

HTML Code

HTML code is given below.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Current Date and Time is <span id='date-time'></span>.</p>
</body>
</html>

JavaScript Code

Javascript code is given below.

<script>
var dt = new Date();
document.getElementById('date-time').innerHTML=dt;
</script>

Demo

Current Date and Time is .

Video

Watch video tutorial on displaying date and time using javascript and html.