Automatic Time Kept In A From

[Total: 0    Average: 0/5]

This script shows show to use the Date object, make some manipulations on the date object and update a form textbox every second to keep the time current.

Head Code:

<SCRIPT type=text/javascript>
  TDay = new Array();
  TDay[0] = "Sunday";
  TDay[1] = "Monday";
  TDay[2] = "Tuesday";
  TDay[3] = "Wednesday";
  TDay[4] = "Thursday";
  TDay[5] = "Friday";
  TDay[6] = "Saturday"; 
  TMonth = new Array();
  TMonth[0] = "January";
  TMonth[1] = "February";
  TMonth[2] = "March";
  TMonth[3] = "April";
  TMonth[4] = "May";
  TMonth[5] = "June";
  TMonth[6] = "July";
  TMonth[7] = "August";
  TMonth[8] = "September";
  TMonth[9] = "October";
  TMonth[10] = "November";
  TMonth[11] = "December";
  TDate = new Date();
  CurYear  = TDate.getYear();
  CurYear=(CurYear<2000)?1900+CurYear:CurYear;
  CurMonth = TDate.getMonth();
  CurDayOw = TDate.getDay();
  CurDay   = TDate.getDate();
  TheDate  = TDay[CurDayOw] + ', ';
  TheDate += TMonth[CurMonth] + ' ';
  TheDate += CurDay + ', ';  
  TheDate += CurYear;

function trackTime()
{
  TTime = new Date();
  CurHour = TTime.getHours();
  CurMin  = TTime.getMinutes();
  CurSec  = TTime.getSeconds();
  TheTime  = CurHour;// Add them
  TheTime += ((CurMin < 10)?':0':':') + CurMin;
  TheTime += ((CurSec < 10)?':0':':') + CurSec;
  document.examplef.time_box.value = TheTime;
}
setInterval('trackTime()',1000);
</SCRIPT>

Body Code:

<script type=text/javascript>
document.write(TheDate+'<BR>');
</script>
<FORM NAME="examplef">
Time : <INPUT type=text NAME="time_box" SIZE=10>
</FORM>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *