Display Current Date And Time In HTML By JavaScript

Display Current Date And Time In HTML By JavaScript

Here we show how you can display current date and time in your web page with the help of simple java script code.

Sometime we need to display current date and time in our html code, it is a very common task to show current date and time in your website.

Create the Date Object

     Date objects are created with the new Date() constructor.

				
					<script>
    var currentday = new Date();
</script>
				
			

Get Method to Show the current Date in JavaScript

      If you want to get the date in the YYYY-MM-DD format. Use below code:

				
					<script>
    var currentday = new Date();
    var date = currentday.getFullYear() +'-'+ (currentday.getMonth()+1) +'-'+ currentday.getDate();
</script>
				
			

         getFullYear()– Provides current year like 2021.

         getMonth()– Provides current month values 0-11. Where 0 for Jan and 11 for Dec. So added +1 to get result.

          getDate()– Provides day of the month values 1-31.

 

      Below code for show current date in HTML.

				
					<html>
	<head>
	  <title>Inflay.com | Display Current Date And Time In HTML By JavaScript</title>
	</head>
    <body>
     <h1>Get current date using JavaScript.</h1>
 
     <h2 id="displaydate"></h2>
    </body>
    <script type="text/javascript">
	  var currentday = new Date();
	  var date = currentday.getFullYear()+'-'+(currentday.getMonth()+1)+'-'+currentday.getDate();
	  document.getElementById("displaydate").innerHTML = date;
    </script>
</html>
				
			

Display Hours, Minutes, and Seconds using JavaScript

      Use the following script to get the current time using JavaScript in HH:MM:SS format.

				
					<script>
    var currentday = new Date();
    var time = currentday.getHours() + ":" + currentday.getMinutes() + ":" + currentday.getSeconds();
</script>
				
			

         getHours()– Provides current hour between 0-23.
         getMinutes()– Provides current minutes between 0-59.
         getSeconds()– Provides current seconds between 0-59.

       Below code for show current time in HTML.

				
					<html>
	<head>
	  <title>Inflay.com | Display Current Date And Time In HTML By JavaScript</title>
	</head>
<body>
  <h1>Get current time using JavaScript.</h1>
 
  <h2 id="displaytime"></h2>
</body>
  <script type="text/javascript">
	  var currentday = new Date();
	  var time = currentday.getHours() + ":" + currentday.getMinutes() + ":" + currentday.getSeconds();
	  document.getElementById("displaytime").innerHTML = time;
  </script>
</html>
				
			

Display the Full Current Date and Time in JavaScript

       show full date and time in the YYYY-MM-DD and HH:MM:SS formats.

    Below Code Show Current Date and  Time Both in JavaScript

				
					<html>
	<head>
	  <title>Inflay.com | Display Current Date And Time In HTML By JavaScript</title>
	</head>
<body>
  <h1>Get current date and time using JavaScript.</h1>
 
  <h2 id="displaydatetime"></h2>
</body>
  <script type="text/javascript">
	var currentday = new Date();
	var date = currentday.getFullYear()+'-'+(currentday.getMonth()+1)+'-'+currentday.getDate();
	var time = currentday.getHours() + ":" + currentday.getMinutes() + ":" + currentday.getSeconds();
	var datetime = date+' '+time;
	
	document.getElementById("displaydatetime").innerHTML = datetime;
  </script>
</html>
				
			

I hope this article helps you.
Thanks for visiting Inflay.com .