PDA

View Full Version : Time in a input



Rockonmetal
03-31-2007, 03:47 PM
Does anyone know how to make the current time display in a text input???
I want it in a input! Thanks!

REALLY APPRECIATE IT!

mburt
03-31-2007, 03:57 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Input Clock</title>
<script type="text/javascript">
function st() {
var c = document.getElementById("clock");
var date = new Date(),
hr = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
var mS = m < 10 ? "0" : "",
sS = s < 10 ? "0" : "";
var hh = hr > 12 ? hr-12 : hr;
c.value = hh+":"+mS+m+":"+sS+s
setTimeout(function() {st();},1000);
}
window.onload=st;
</script>
</head>
<body>
<input id="clock">
</body>
</html>

Twey
03-31-2007, 04:12 PM
I don't think he wanted it updated, if he did there wouldn't be much point in putting it in an input.
<input type="text" id="clock">
<script type="text/javascript">
Number.prototype.pad = function(n) {
for(var r = this.toString(); r.length < n; r = "0" + r);
return r;
};
var d = new Date();
document.getElementById("clock").value = [d.getHours().pad(2), d.getMinutes().pad(2), d.getSeconds().pad(2)].join(":");
</script>
setTimeout("st()",1000);That's very ugly, not least because you have to get a reference to the clock all over again. Use a closure:
function updateClock(cl) {
var d = new Date();
(cl || cl = document.getElementById("clock")).value = [d.getHours().pad(2), d.getMinutes().pad(2), d.getSeconds().pad(2)].join(":");
setTimeout(function() { updateClock(cl); }, 1000);
}
updateClock();

Rockonmetal
03-31-2007, 04:57 PM
I don't care if it updates, thanks Mb thats gonna help alot. (people have been sending in a lot of forms in to little time (clogging up my email)

pcbrainbuster
03-31-2007, 05:02 PM
Thats a little harsh don't you think .... ( you didn't have to say it like that )