Click to See Complete Forum and Search --> : auto generate date in textfield !!
tuanyong
08-31-2003, 08:53 PM
i did it with time but for date im having some problems ...
the code for time is as follows ...
<script type="text/javascript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var time = new Date()
var hours = time.getHours()
var minutes = time.getMinutes()
minutes=((minutes < 10) ? "0" : "") + minutes
var seconds = time.getSeconds()
seconds=((seconds < 10) ? "0" : "") + seconds
var clock = hours + ":" + minutes + ":" + seconds
document.forms[0].display.value = clock
timer = setTimeout("start()",1000)
}
</script>
<body onload="start()" onunload="stop()">
<input type="text" name="display" size="10">
could someone try to modify and have the date auto generated in a textfield too ... thanks
freefall
08-31-2003, 10:06 PM
Here's just about everything you'll ever need. I made a comment a to where you can change how it displays and what it displays, I think it is fairly self explanatory, if you need help, thats fine just throw a post.
<script type="text/javascript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var year = d.getYear();
var month = d.getMonth();
var date = d.getDate();
var day = d.getDay();
var ampm=((hours > 11) ? "PM" : "AM");
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
hours-=((hours > 11) ? 12 : 0);
hours=((hours == 0) ? 12 : hours);
hours=((hours < 10) ? " " : "") + hours;
minutes=((minutes < 10) ? "0" : "") + minutes;
seconds=((seconds < 10) ? "0" : "") + seconds;
// Take out whatever you don't want to be displayed in the following line:
document.forms[0].display.value = weekday[day] + ", " + monthname[month] + " " + date + ", " + year + " " + hours + ":" + minutes + ":" + seconds + " " + ampm;
timer = setTimeout("start()",1000)
}
</script>
<body onload="start()" onunload="stop()">
<form>
<input type="text" name="display" size="40">
</form>
This generates your text box as follows (example):
Sunday, Aug 31, 2003 11:05:25 PM
You can play around with it, good luck and God bless,
Ian
tuanyong
08-31-2003, 10:33 PM
erm .. what if i want to separate date and time into 2 textfields.. such that im running 2 scripts at the same time ..
the output of the date is something like this
Time : 11:24:55(textfield)
Date : 1.9.2003(textfield)
i understand that you consolidate everything into a textfield
in your previous reply ..
so can you do me one more favor by separating them into 2..
thank you very much ...
hope ya don't mind
freefall
09-01-2003, 12:31 AM
<script type="text/javascript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var year = d.getYear();
var month = d.getMonth();
var date = d.getDate();
var day = d.getDay();
var ampm=((hours > 11) ? "PM" : "AM");
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
hours-=((hours > 11) ? 12 : 0);
hours=((hours == 0) ? 12 : hours);
hours=((hours < 10) ? " " : "") + hours;
minutes=((minutes < 10) ? "0" : "") + minutes;
seconds=((seconds < 10) ? "0" : "") + seconds;
document.forms[0].time.value = hours + ":" + minutes + ":" + seconds + " " + ampm;
document.forms[0].date.value = weekday[day] + ", " + monthname[month] + " " + date + ", " + year;
timer = setTimeout("start()",1000)
}
</script>
<body onload="start()" onunload="stop()">
<form>
<input type="text" name="time" size="10">
<input type="text" name="date" size="20">
</form>
Charles
09-01-2003, 06:21 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
onload = function () {setInterval("document.getElementById('date').value = new Date().toDateString(); document.getElementById('time').value = new Date().toTimeString()", 100)}
// -->
</script>
<input type="text" id="date">
<input type="text" id="time">
tuanyong
09-01-2003, 09:31 AM
hi .. thanks for your help, Charles and freefall ....
finally i have managed to figure out how to separate them ..10
freefall
09-01-2003, 10:23 AM
Thanks for that shortened version, Charles. I'm really not good at condensing code like that, I'm still thinking in a C++ mindset =)
- Ian
Charles
09-01-2003, 10:44 AM
It really is a different mindset. In JavaScript every line of code and everything that you give a name to has a really big overhead.
freefall
09-01-2003, 01:32 PM
Yeah, I'll get there some day... my current website runs on about 600 lines of hand-coded, original Javascript... that's way too much (I'm remaking the site right now along with the Javascript!), but it could probably be condensed to 100 or 200 lines. Oh well,
God bless,
Ian