How to Display Current Date and Time in HTML using JavaScript?

26-May-2023

.

Admin

How to Display Current Date and Time in HTML using JavaScript?

In this tutorial, you will learn javascript to display the current date and time. This post will give you simple example of javascript to display the current date and time. step by step explain javascript date objects. This tutorial will give you simple example of display current date and time in html using javascript. So, let's follow few step to create example of how to get the current date and time in javascript.

To display the current date and time in HTML using JavaScript, you can use the following code:

Example 1:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Display Current Date and Time in HTML using JavaScript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

<p id="datetime"></p>

<script>

var dt = new Date();

document.getElementById("datetime").innerHTML = dt.toLocaleString();

</script>

</script>

</html>

In this code, we have created a paragraph element with an ID of "datetime". We then use JavaScript to get the current date and time by creating a new Date object. We then set the innerHTML of the paragraph element to the formatted string representation of the date and time using the toLocaleString() method.

When you open this HTML file in a web browser, it will display the current date and time in your local timezone.

#JavaScript