
January 11th, 2008, 11:52 AM
|
|
|
|
Insert current time into a form field
I spent quite a while looking for something like this and finally managed to piece it together, using bits of code here and there. I'm still learning this, so it might not be that impressive to some, but I'm sure someone will find it useful.
Code:
<script language="JavaScript" type="text/JavaScript">
function inserttime(time)
{
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
curr_hour = checkTime(curr_hour);
curr_min = checkTime(curr_min);
time.value = curr_hour + ":" + curr_min
}
function checkTime(i)
{
if (i < 10)
{
i = "0" + i;
}
return i;
}
</script>
Then simply call it with onClick="inserttime(document.{form name}.{field name}). This particular code assumes 24-hour display.
Edit: Had to move the hour/min check outside the "inserttime" function in order for it to work correctly.
|