+ 1
how do i dispay time
I want to make a button so that when i click it it displays time. also add how i can just display time without a button
5 Answers
+ 1
<script>
function printTime() {
var d = new Date();
var hours = d.getHours();
var mins = ("0" + d.getMinutes()).slice(-2);
var secs = ("0" + d.getSeconds()).slice(-2);
document.body.innerHTML = hours+":"+mins+":"+secs;
}
setInterval(printTime, 1000);
</script>
+ 1
With a button (simple method):
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
body {
padding: 60px;
background-color: #000;
color: lime;
font-size: 46pt;
}
</style>
<script>
function printTime() {
var d = new Date();
var hours = d.getHours();
var mins = ("0" + d.getMinutes()).slice(-2);
var secs = ("0" + d.getSeconds()).slice(-2);
document.body.innerHTML = hours+":"+mins+":"+secs;
setInterval(printTime, 1000);
}
</script>
</head>
<body>
<button onClick="printTime()">Show Time</button>
</body>
</html>
+ 1
thanks a bunch man you have helped a lot
0
use getTime()
0
Farhan, Thx anyway but i wanted a working clock, however your info will help in future