Does anyone know how to make the current time display in a text input???
I want it in a input! Thanks!
REALLY APPRECIATE IT!
Does anyone know how to make the current time display in a text input???
I want it in a input! Thanks!
REALLY APPRECIATE IT!
Code:<!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>
Last edited by mburt; 03-31-2007 at 06:43 PM.
- Mike
I don't think he wanted it updated, if he did there wouldn't be much point in putting it in an input.Code:<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>That's very ugly, not least because you have to get a reference to the clock all over again. Use a closure:Code:setTimeout("st()",1000);Code: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();
Last edited by Twey; 03-31-2007 at 04:24 PM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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)
Thats a little harsh don't you think .... ( you didn't have to say it like that )
Bookmarks