JavaScript Status clock - Advance
In previous article JavaScript Status clock – Basic we see simple technique to add clock at browser status bar. Now we are going to manipulate data which we get from Date object constructor. Using the date object and some of its methods, you can create your own JavaScript clock customized to our needs.Here we are going to use some method supported by Date object are as follows…
| Table 1 Methods we have used in our example | |
| Method Function | What the Function Does |
| getHours() | Returns the current number of hours (e.g. 0-23) |
| getMinutes() | Returns the current number of minutes (e.g. 0-59) |
| getSeconds() | Returns the current number of seconds (e.g. 0-59) |
| getDate() | Returns the day of the month (e.g. 1-31) |
| getMonth() | Returns the number of months (e.g. 0-11) |
| getDay() | Returns the day of week (e.g. 0-6) |
| getFullYear() | Returns the year, as a four-digit number (e.g. 2007) |
First of all we define variable var currentDate=new Date() and create a date object. An advantage of using objects is that objects can hold multiple values, where variables can only keep one value at a time. And then using above method get the required parameter stored in variable.
var TimeStamp, month, period, day, year;
hour = currentDate.getHours()
min = currentDate.getMinutes()
sec = currentDate.getSeconds()
year = currentDate.getFullYear()
date = currentDate.getDate()
switch(currentDate.getDay()){
case 0:day="Sunday";break;
case 1:day="Monday";break;
case 2:day="Tuesday";break;
case 3:day="Wednesday";break;
case 4:day="Thursday";break;
case 5:day="Friday";break;
case 6:day="Saterday";break;
}
if (hour>12){hour-=12;period="pm"} else {period="am"}
if (currentDate.getHours()==12){period="pm"}
if (currentDate.getHours()==24){period="am"}
<script type="text/javascript">
function time(){
var currentDate=new Date()
var TimeStamp,month,period,day,year;
hour=currentDate.getHours()
min=currentDate.getMinutes()
sec=currentDate.getSeconds()
year=currentDate.getFullYear()
date=currentDate.getDate()
switch(currentDate.getDay()){
case 0:day="Sunday";break;
case 1:day="Monday";break;
case 2:day="Tuesday";break;
case 3:day="Wednesday";break;
case 4:day="Thursday";break;
case 5:day="Friday";break;
case 6:day="Saterday";break;
}
switch(currentDate.getMonth()){
case 0:month="January";break;
case 1:month="Febuary";break;
case 2:month="March";break;
case 3:month="April";break;
case 4:month="May";break;
case 5:month="June";break;
case 6:month="July";break;
case 7:month="August";break;
case 8:month="September";break;
case 9:month="October";break;
case 10:month="November";break;
case 11:month="December";break;
}
if(sec<10){sec="0"+sec}
if(min<10){min="0"+min}
if(hour>12){hour-=12;period="pm"} else {period="am"}
if(currentDate.getHours()==12){period="pm"}
if(currentDate.getHours()==24){period="am"}
var TimeStamp=day+" "+month+" "+date+" "+year+" "+hour+":"+min+":"+sec+" "+period
window.status=TimeStamp
window.setTimeout("time()",300)
}
time()
</script>
Example:
Please download source code to see example...

written by karthikeyaan, March 15, 2009
written by abdul Rauf (RuFi), April 15, 2009
Best Regards,
Abdul Rauf
written by Chetankumar Akarte, April 15, 2009
Just copy the detail script and paste it anywhere in Body or Head section of your web page.
Regards
Chetankumar Akarte









