How to Get Current Time in Javascript?

Jul 16, 2021 . Admin

Hello Friends, Now let's see example of how to use javascript get current time example. We will use how to use if get current time in javascript. Here you will learn how to use javascript get current time. This is a short guide on get current time. Let's get started with how to get current time in javascript. Here i will give you many example how you can check javascript get current time. Example: 1
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>How To Get Current Time In Javascript?</title>
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	</head>
	<body class="bg-dark">
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h5>How To Get Current Time In Javascript?</h5>
						</div>
						<div class="card-body">
							<div id="time"></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			function checkTime(i) {
			  	if (i < 10) {
			    	i = "0" + i;
			  	}
			  	return i;
			}
			function startTime() {
			  	var today = new Date();
			  	var h = today.getHours();
			  	var m = today.getMinutes();
			  	var s = today.getSeconds();
			  	// add a zero in front of numbers<10
			  	m = checkTime(m);
			  	s = checkTime(s);
			  	document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
			  	t = setTimeout(function() {
			    	startTime()
			  	}, 500);
			}
			startTime();	
		</script>
	</body>
</html>
Output:
15:44:12
Example: 2
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>How To Get Current Time In Javascript?</title>
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
		<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
	</head>
	<body class="bg-dark">
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h5>How To Get Current Time In Javascript?</h5>
						</div>
						<div class="card-body">
							<div id="time"></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			function realtime() {
			  	let time = moment().format('h:mm:ss a');
			  	document.getElementById('time').innerHTML = time;
			  	setInterval(() => {
			    	time = moment().format('h:mm:ss a');
			    	document.getElementById('time').innerHTML = time;
				}, 1000)
			}
			realtime();	
		</script>
	</body>
</html>
Output:
3:45:22

It will help you....

#Javascript