3-5 �������

¤é´Áª«¥ó¬O¥t¤@Ãþ JavaScript ªº¤º«Øª«¥ó¡A§Ú­Ì¥i¥Î new Date() ¨Ó²£¥Í¤@­Ó¤é´Áª«¥ó¡A¨Ã¥Î¦UºØ¤èªk¨Ó¨ú¥X¦¹ª«¥óªº¬ÛÃö¸ê°T¡A¦p¤é´Á¡B®É¼Æ¡B¬í¼Æµ¥¡C¨Ò¦p¡G

Example¡]date01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]date01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¤é´Áª«¥óªº¤èªk</h2>
<hr>

<script>
today = new Date();
document.write("<br>¼Ð·Ç¦r¦ê¡Gtoday.toString() = "+today.toString());
document.write("<br>¦a¤è¦r¦ê¡Gtoday.toLocaleString() = "+today.toLocaleString());
document.write("<br>¦~¥÷¡Gtoday.getFullYear() = "+today.getFullYear());
document.write("<br>¤ë¥÷¡Gtoday.getMonth() = "+today.getMonth());
document.write("<br>¤é¼Æ¡Gtoday.getDate() = "+today.getDate());
document.write("<br>®É¼Æ¡Gtoday.getHours() = "+today.getHours());
document.write("<br>¤À¼Æ¡Gtoday.getMinutes() = "+today.getMinutes());
document.write("<br>¬í¼Æ¡Gtoday.getSeconds() = "+today.getSeconds());
document.write("<br>¬P´Á´X¡Gtoday.getDay() = "+today.getDay());
</script>

<hr>
</body>
</html>

¦b¤W¨Ò¤¤¡Anew Date() §Y·|²£¥Í¥]§t²{¦b¤é´Áªº¤@­Ó¤é´Áª«¥ó¡A±`¥Îªº¤èªk¥i¥H¦Cªí»¡©ú¦p¤U¡G

ª«¥ó¤èªk»¡©ú
DatetoString()¥H¼Ð·Ç¦r¦ê¨Óªí¥Ü¤é´Áª«¥ó
toLocaleString()¥H¦a¤è¦r¦ê¡]¨Ì§@·~¨t²Î¦Ó¦³©Ò¤£¦P¡^¨Óªí¥Ü¤é´Áª«¥ó
getYear()¨ú±o¦è¤¸¦~¥÷¡]¦¹¤èªk¤w¸g¹L®É¡A·|¨ÌÂsÄý¾¹¦Ó¦³¤£¦Pµ²ªG¡A«Øij¤£­n¨Ï¥Î¡^
getFullYear()¨ú±o§¹¾ã¦è¤¸¦~¥÷
getMonth()¨ú±o¤ë¥÷¡]»Ýª`·N¡G0 ¥Nªí¤@¤ë¡A¦]¦¹¨Ò¦p­Y¬O¤K¤ë¡Aµ²ªG´N¬O 7¡^
getDate()¨ú±o¤é¼Æ
getHours()¨ú±o®É¼Æ
getMinutes()¨ú±o¤ÀÄÁ¼Æ
getSeconds()¨ú±o¬í¼Æ
getDay()¨ú±o¬P´Á¼Æ¡]¨Ò¦p­Y¬O¬P´Á¥|¡Aµ²ªG´N¬O 4¡F­Y¬O¬P´Á¤é¡Aµ²ªG´N¬O 0¡^

¨Æ¹ê¤W¦³Ãö¤é´Áª«¥óªº¤èªk¬Û·í¦h¡A¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î eval() ¨Ó²¤Æµ{¦¡¡A¨Ã¹ï³o¨Ç¤èªk¶i¦æ¸û§¹¾ãªº¦Cªí¡G

Example¡]date02.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]date02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¤é´Áª«¥óªº¤èªk¡G¨Ï¥Î eval()</h2>
<hr>

<script>
today = new Date();		// «Ø¥ßDateª«¥ó
// ¦C¥X»P Date ª«¥óªº¤@¨Ç¤èªk
method =["getDate", "getDay", "getFullYear", "getYear", "getHours", "getMilliseconds",
	"getMinutes", "getMonth", "getSeconds", "getTime", "getTimezoneOffset",
	"getUTCDate", "getUTCDay", "getUTCFullYear", "getUTCHours", "getUTCMilliseconds",
	"getUTCMinutes", "getUTCMonth", "getUTCSeconds", "toGMTString", "toLocaleString",
	"toUTCString", "toString", "valueOf"];

// ¦C¥X°õ¦æ³o¨Ç¤èªk«á±o¨ìªºµ²ªG
for (i=0; i<method.length; i++)
	document.writeln("<br> today." + method[i] + "() = " + eval("today."+method[i]+"();"));
</script>

<hr>
</body>
</html>

Hint
  • UTC ¥Nªí¡u¥@¬É®É¶¡®y¼Ð¡v¡]Universal Time Coordinate¡^¡C
  • GMT ¥Nªí¡u®æªL«Âªv¼Ð·Ç®É¶¡¡v¡]Greenwich Mean Time¡^¡C

¦b¤W­z¤èªk¤¤¡A³Ì±`¥Î¨ìªº´N¬O getTime()¡A¦¹¤èªk¥i¥H¶Ç¦^¨Ó¬Y­Ó®É¶¡ª«¥ó»P 1970 ¦~ 1 ¤ë 1 ¤é¹s®Éªº®É¶¡®t¶Z¡A³æ¦ì¬O 1/1000 ¬í¡C¨Ò¦p¡A§Ú­Ì¥i¥H¨Ï¥Î³o­Ó¤èªk¡A¨Ó¼g¥X¤@­Ó²©öªº°¨¿öµ{¦¡¡A¦p¤U¡G

Example¡]dateGetTime01.htm¡^¡G

·í§A«ö¤U¡u¶}©l­p®É¡v«á¡Aµ{¦¡½X¤º³¡¥ß§Y¶}©l­p®É¡Aª½¨ì§A«ö¤U¡u°±®É­p®É¡v®É¡A´N·|¦b¤å¦rÄæ¦ìÅã¥Ü¸g¹L®É¶¡¡C¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]dateGetTime01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<body>
<h2 align=center>²©ö°¨¿ö</h2>
<hr>

<script language=javascript>
function go(form){
	switch(form.myButton.value){
		case "¶}©l­p®É":
			date1=new Date();				// ¨ú±o¥Ø«e®É¶¡
			form.timeDisplay.value="­p®É¤¤...";		// ³]©w®É¶¡Åã¥Ü¬° ­p®É¤¤...
			form.myButton.value="°±¤î­p®É";			// ±N«ö¶s¤å¦r§ï¬°¡u°±¤î­p®É¡v
			break;
		case "°±¤î­p®É":
			date2=new Date();				// ¨ú±o¥Ø«e®É¶¡
			timeDiff=(date2.getTime()-date1.getTime())/1000;// ®É¶¡®t¶Z¡A¥H¬í¬°³æ¦ì
			form.timeDisplay.value=timeDiff;		// ³]©w®É¶¡Åã¥Ü
			form.myButton.value="Âk¹s";			// ±N«ö¶s¤å¦r§ï¬°¡uÂk¹s¡v
			break;
		case "Âk¹s":
			form.timeDisplay.value="00:00:00";		// ³]©w®É¶¡Åã¥Ü¬° 00:00:00
			form.myButton.value="¶}©l­p®É";			// ±N«ö¶s¤å¦r§ï¬°¡u¶}©l­p®É¡v
			break;
		default:
			alert("Error!");
			break;
	}
}
</script>
<center>
<form>
<input type="button" name="myButton" value="¶}©l­p®É" onClick="go(this.form)">
<p>¸g¹L®É¶¡¡G<input type=text name="timeDisplay" value="00:00:00">¬í
</form>
</center>

</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î«ö¶s¤¤ªº¤å¦r¨Ó¥Nªí¨t²Îª¬ºA¡A¦b¤£¦Pªºª¬ºA¤U¡A«ö¤U«ö¶s·|¦³¤£¦Pªº¤ÏÀ³¡A»¡©ú¦p¤U¡G

¤W­z¬yµ{¡A¥i¥Î¹Ï§ÎÅã¥Ü¦p¤U¡G

Example¡]dateGetTime01.ppt¡^¡G

Hint
·íµM¡A§A¤]¥i¥H³]­p¦b­p®É¹Lµ{¤¤¡A¤å¦rÄæ¦ì¤£Â_Åã¥Ü¥Ø«e­p®Éµ²ªG¡A¦ý³oºØ±K¶°ªº§Y®ÉÅã¥Ü·|¤ñ¸û®ø¯Ó CPU ªº­pºâ®É¶¡¡C±ý¹F¨ì¦¹¥\¯à¡A¥²¶·¨Ï¥Î setTimeOut() ¨ç¼Æ¡A±N·|¦b«á­±¸Ô­z¡C

§Ú­Ì¤]¥i¥H²£¥Í¤@­Ó¤é´Áª«¥ó«á¡A¦A¹ï¦¹ª«¥ó¶i¦æ­×§ï¡]¨Ò¦p§ïÅܬí¼Æ©Î¤ë¥÷¡^©Î®æ¦¡Âà´«¡A¬ÛÃöªº¤èªk«Ü¦h¡A¥]§t setDate¡BsetFullYear¡BsetHours¡BsetMilliseconds¡BsetMinutes¡BsetMonth¡BsetSeconds¡BsetTime¡BsetUTCDate¡BsetUTCFullYear¡BsetUTCHours¡BsetUTCMilliseconds¡BsetUTCMinutes¡BsetUTCMonth¡BsetUTCSeconds¡BsetYear µ¥¡A¦b¦¹¤£¦AÂØ­z¡C


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ