+ 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

1st Jul 2016, 10:13 PM
aiden
aiden - avatar
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>
2nd Jul 2016, 3:31 AM
ZinC
ZinC - avatar
+ 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>
2nd Jul 2016, 3:44 AM
ZinC
ZinC - avatar
+ 1
thanks a bunch man you have helped a lot
2nd Jul 2016, 7:23 AM
aiden
aiden - avatar
0
use getTime()
5th Jul 2016, 6:14 PM
Farhan Yahya
Farhan Yahya - avatar
0
Farhan, Thx anyway but i wanted a working clock, however your info will help in future
5th Jul 2016, 6:52 PM
aiden
aiden - avatar