|
One thing to know before you start: all of these read the clock on your
visitor's computer, not some official clock out on the Internet. If their clock
is wrong, yours is too. My uncle's computer thinks it's January 1980 every time he turns
it on, because the little battery inside is dead.
1. A CLOCK THAT TICKS
Here it is, running:
It's just a text box that the script rewrites once a second. This part goes in your
<head>:
<script language="JavaScript">
<!--
// Ticking clock - Lorenzo's JavaScript Zone
function runClock() {
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var secs = now.getSeconds();
var ampm = "AM";
if (hours >= 12) { ampm = "PM"; }
if (hours > 12) { hours = hours - 12; }
if (hours == 0) { hours = 12; }
if (mins < 10) { mins = "0" + mins; }
if (secs < 10) { secs = "0" + secs; }
document.clockForm.face.value = hours + ":" + mins + ":" + secs + " " + ampm;
setTimeout("runClock()", 1000);
}
//-->
</script>
|
This goes in your body where you want the clock:
<form name="clockForm" onSubmit="return false">
<input type="text" name="face" size="13" onFocus="this.blur()">
</form>
|
And start it with <body onLoad="runClock()">.
- Why the if (mins < 10) lines? Without them, 3:05 comes out as
"3:5". My friend Landon found that one the first week, at five after three, of
course.
- onFocus="this.blur()" kicks the cursor out if somebody clicks
in the box, so they can't type over your clock.
- Want 24-hour time? Take out the three lines with ampm and
hours - 12 and hours == 0, and take + " " + ampm off
the end of the long line.
- Don't rename the form without also changing clockForm in the script.
They have to match exactly. Capital F and everything.
2. TODAY'S DATE (Y2K READY!)
Writes something like Today is Thursday, September 23, 1999 right on your page.
Put it in your body wherever you want it:
<script language="JavaScript">
<!--
// Today's date - Lorenzo's JavaScript Zone
var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var monthNames = new Array("January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December");
var today = new Date();
var year = today.getYear();
if (year < 1000) { year = year + 1900; }
document.write("Today is " + dayNames[today.getDay()] + ", "
+ monthNames[today.getMonth()] + " " + today.getDate() + ", " + year);
//-->
</script>
|
The Y2K part. Right now getYear() gives you 99, not 1999. Come January,
depending on the browser, it'll give you either 100 or 2000. That's the line
if (year < 1000): if the number is small, add 1900, and you get the right year
either way. Scripts that just stick "19" in front are going to say
"19100" (or "192000"!) on New Year's Day, and I bet you see a bunch of them. Newer browsers also
have getFullYear(), which gives all four digits, but older Netscapes don't know
it, and a script that crashes is worse than no script. So I use the fix.
Also: getMonth() counts from 0. January is 0, December is 11. That's
why the month names are in a list: monthNames[0] is January. Same thing for days,
where Sunday is 0.
3. GOOD MORNING / GOOD EVENING
<script language="JavaScript">
<!--
// Greeting - Lorenzo's JavaScript Zone
var hr = new Date().getHours();
if (hr < 12) {
document.write("Good morning!");
} else if (hr < 18) {
document.write("Good afternoon!");
} else {
document.write("Good evening!");
}
//-->
</script>
|
getHours() goes from 0 (midnight) to 23 (11 at night), so 18 is 6 PM.
Change the words to whatever you want. Mine says "Go to bed!" after midnight but
my mom made me take it off.
4. Y2K COUNTDOWN
Counts the days left until January 1, 2000. The night I finished it, it said 100.
<script language="JavaScript">
<!--
// Y2K countdown - Lorenzo's JavaScript Zone
var now = new Date();
var bigDay = new Date(2000, 0, 1);
var msPerDay = 24 * 60 * 60 * 1000;
var daysLeft = Math.ceil((bigDay.getTime() - now.getTime()) / msPerDay);
if (daysLeft > 1) {
document.write("Only " + daysLeft + " days until the year 2000!");
} else if (daysLeft == 1) {
document.write("The year 2000 is TOMORROW!");
} else {
document.write("Welcome to the year 2000! Did your computer make it?");
}
//-->
</script>
|
- new Date(2000, 0, 1) is January 1, 2000. Remember, January is month 0. I
spent a whole hour wondering why mine was counting down to February.
- getTime() gives you a date in milliseconds, so you subtract the two and divide
by how many milliseconds are in a day.
- Math.ceil rounds up, so on the afternoon of December 31 it says tomorrow instead
of zero.
- It already knows what to say after New Year's, so you don't have to rush and fix
your page at midnight. You'll be busy.
NEXT: NO RIGHT CLICK >>
|