מהו זמן?
זוהי שאלה טיפה פילוסופית, אבל בואו נחשוב עליה לעומק.
זמן זה מה שאנחנו רוצים בשביל לדייק, כלומר אתה לא מגיע בזמן לבית ספר, למה? בגלל שעבר הזמן
איך מציגים את הזמן?
מציגים את הזמן בעזרת האפשרות הנחמדה של-JS השיטה Date()
איך?
בעזרת שנכתוב כך:
קוד HTML:
<script language="JavaScript" type="text/javascript"> document.write(Date()); </script>
נעתיק את הקוד ונדביק מה יצא?
לי יצא כך:
Sun Jan 15 2012 17:16:14 GMT+0200 (Jerusalem Standard Time)
אבל זה אצלי כך מכיוון שהזמן מתעדכן.
עכשיו אנחנו הצגנו זמן 
הצגת זמן המשתמש.
איך נוכל להציג למשתמש למשל:
בוקר טוב השעה היא: X?
איך עושים את זה?
באילו פקודות וכ'ו.
בשביל לכתוב תוכנית כזאת נכתוב כך:
<script language="JavaScript" type="text/javascript">
<!--
var d = new Date() ;
var time = d.getHours();
document.write("<span style='color: red;'>");
document.write("<div style='text-align: center;'>");
if ((time >= "5") && (time <= "10" ))
{
document.write("Good Morning! ");
}
else if ((time >= "11") && (time <= "16"))
{
document.write("Good afternoon! ");
}
else if ((time >= "17") && (time <= "20"))
{
document.write("Good Evening! ");
}
else if ((time >= "20") && (time <= "24"))
{
document.write("Good Night! ");
}
document.write("</span>");
document.write("<span style='color: orange;'>");
switch (time)
{
case 1:
document.write("The time now is: 1:00");
break;
case 2:
document.write("The time now is: 2:00");
break;
case 3:
document.write("The time now is: 3:00");
break;
case 4:
document.write("The time now is: 4:00");
break;
case 5:
document.write("The time now is: 5:00");
break;
case 6:
document.write("The time now is: 6:00");
break;
case 7:
document.write("The time now is: 7:00");
break;
case 8:
document.write("The time now is: 8:00");
break;
case 9:
document.write("The time now is: 9:00");
break;
case 10:
document.write("The time now is: 10:00");
break;
case 11:
document.write("The time now is: 11:00");
break;
case 12:
document.write("The time now is: 12:00");
break;
case 13:
document.write("The time now is: 13:00");
break;
case 14:
document.write("The time now is: 14:00");
break;
case 15:
document.write("The time now is: 15:00");
break;
case 16:
document.write("The time now is: 16:00");
break;
case 17:
document.write("The time now is: 17:00");
break;
case 18:
document.write("The time now is: 18:00");
break;
case 19:
document.write("The time now is: 19:00");
break;
case 20:
document.write("The time now is: 20:00");
break;
case 21:
document.write("The time now is: 21:00");
break;
case 22:
document.write("The time now is: 22:00");
break;
case 23:
document.write("The time now is: 23:00");
break;
case 24:
document.write("The time now is: 24:00");
break;
}
document.write("</span>");
document.write("</div>");
//-->
</script>
אז מה בעצם רשמנו פה?
קודם כל כתבנו שני משתנים: המשתנה d והמשתנה time, במשתנה d הכנסנו תאריך כמו שלמדנו מקודם ובמשתנה time הכנסנו את המשתנה t ועשינו ספירה ע"פ השעות.
אחרי זה עשינו בדיקה:
אם זה בוקר אז זה ירשום: Good Morning!
אם זה צהריים זה ירשום: Good afternoon!
אם זה ערב זה ירשום: Good Evening!
אם זה לילה זה ירשום: Good Night!
אחרי זה ביצענו בדיקת switch שבודקת כל שעה ואז זה ירשום (לדוגמה):
The time now is: 1:00
זה יקרה אם המשתמש יכנס לדף באחת לפנות בוקר.
אם תכנס לתוכנית עכשיו (5 בערב אצלי) זה ירשום כך:
Good Evening! The time now is: 17:00זהו!
קל לא? 