Math 與 Date 物件

Math 物件

屬性

E 屬性
<SCRIPT language="JavaScript">
<!--
    window.alert(Math.E);
//-->
</SCRIPT>


範例

Html Source Browser Display
LN10 屬性
LN2 屬性
LOG10E 屬性
LOG2E 屬性
PI 屬性
SQRT2 屬性
SQRT1_2 屬性

方法

基本的方法

這些方法都只有一個參數, 而且回傳一個數值。
計算平方根:
<html>
<head>
<SCRIPT language="JavaScript">
<!--
function get_a_root()
{
    var thenum=window.prompt("Enter a positive number or zero.","");
    var theroot=Math.sqrt(thenum);
    if(thenum<0)
        window.alert("Please Enter a positive number! Try again.");
    else
        window.alert("The square root of "+thenum+" is "+theroot);
}
//-->
</SCRIPT>
</head>
<body>
<form>
<input type="button" value="Get a Square root" onClick="get_a_root();">
</form>
</body>
</html>


範例

Html Source Browser Display

兩個參數的方法

max()和min()方法
<html>
<head>
<SCRIPT language="JavaScript">
<!--
function the_max()
{
    var num1=window.prompt("Enter a number.","");
    var num2=window.prompt("Enter another number.","");
    var largenum=Math.max(num1,num2);
    var smallnum=Math.min(num1,num2);
    if(largenum==smallnum)
        window.alert("Those two numbers are equal!");
    else
        window.alert(largenum+" is larger than "+smallnum);
}
//-->
</SCRIPT>
</head>
<body>
<form>
<input type="button" value="Which Number is Bigger?" onClick="the_max();">
</form>
</body>
</html>


範例

Html Source Browser Display
pow()方法
pow()方法的第一個參數是基數, 第二個參數是次方。
<html>
<head>
<SCRIPT language="JavaScript">
<!--
function the_pow()
{
    var num1=window.prompt("Enter a base number.","");
    var num2=window.prompt("What power should we set it to (a number)?","");
    var theresult=Math.pow(num1,num2);
    window.alert(num1+" to the power of "+num2+" is "+theresult);
}
//-->
</SCRIPT>
</head>
<body>
<form>
<input type="button" value="Find a Power" onClick="the_pow();">
</form>
</body>
</html>


範例

Html Source Browser Display

其他方法

ceil()方法
ceil()方法傳回大於或等於所傳入參數的最小整數。
<html>
<head>
<SCRIPT language="JavaScript">
<!--
function get_ceil()
{
    var num1=window.prompt("Enter a number.","");
    var theceil=Math.ceil(num1);
    window.alert("The ceiling of "+num1+" is "+theceil);
}
//-->
</SCRIPT>
</head>
<body>
<form>
<input type="button" value="Find the Ceiling!" onClick="get_ceil();">
</form>
</body>
</html>


範例

Html Source Browser Display
floor()方法
round()方法

random()方法

random()方法傳回一個介於 0 和 1 之間的隨機浮點數。
隨機整數
要得到一個隨機整數, 可以把 random()方法所得到的結果乘以一個整數, 再刪除小數部分。 例如:
    var rand_num = Math.random()*6;
    var rand_int = Math.floor(rand_num);
即可得到一個介於 0 和 5 之間的隨機整數。
隨機引文
<html>
<head>
<SCRIPT language="JavaScript">
<!--
    var quotes = new Array(10);
    quotes[0] = "學而時習之,不亦說乎?";
    quotes[1] = "巧言令色,鮮矣仁。";
    quotes[2] = "有朋自遠方來,不亦樂乎?";
    quotes[3] = "君子不重, 則不威; 學則不固。";
    quotes[4] = "不患人之不己知, 患不知人也。";
    quotes[5] = "溫故而知新, 可以為師矣。";
    quotes[6] = "學而不思則罔, 思而不學則殆。";
    quotes[7] = "朝聞道, 夕死可矣!";
    quotes[8] = "人不知而不慍,不亦君子乎?";
    quotes[9] = "德不孤, 必有鄰。";
    var rand_int = Math.floor(Math.random()*10);
//-->
</SCRIPT>
</head>
<body>

<H1>子曰</H1>
<SCRIPT language="JavaScript">
<!--
    document.write(quotes[rand_int]);
//-->
</SCRIPT>

</body>
</html>


範例

Html Source Browser Display

子曰

隨機影像
<html>
<head>
<SCRIPT language="JavaScript">
<!--
    var r_images = new Array(10);
    r_images[0] = "/sylee/images/cards/0.bmp";
    r_images[1] = "/sylee/images/cards/1.bmp";
    r_images[2] = "/sylee/images/cards/2.bmp";
    r_images[3] = "/sylee/images/cards/3.bmp";
    r_images[4] = "/sylee/images/cards/4.bmp";
    r_images[5] = "/sylee/images/cards/5.bmp";
    r_images[6] = "/sylee/images/cards/6.bmp";
    r_images[7] = "/sylee/images/cards/7.bmp";
    r_images[8] = "/sylee/images/cards/8.bmp";
    r_images[9] = "/sylee/images/cards/9.bmp";
    var rand_int = Math.floor(Math.random()*10);
//-->
</SCRIPT>
</head>
<body>
<H1>Random Images</H1>
<SCRIPT language="JavaScript">
<!--
    document.write("<img src='" + r_images[rand_int] + "'>");
//-->
</SCRIPT>

</body>
</html>


範例

Html Source Browser Display

Random Images

Date 物件

var rightnow = new Date();

屬性

constructor 屬性

prototype 屬性

方法

取值的方法

getDate()方法
getDay()方法
getHours()方法
getMinutes()方法
getMonth()方法
getSeconds()方法
getTime()方法
getTimezoneOffset()方法
getYear()方法
getFullYear()方法

方法

設值的方法

setDate()方法
setHours()方法
setMinutes()方法
setMonth()方法
setSeconds()方法
setTime()方法
setYear()方法
setFullYear()方法

其他方法

parse()方法
toGMTString()方法
toLocaleString()方法

含 Date 的 Script 程式

在網頁寫日期
<html>
<head>
<SCRIPT language="JavaScript">
<!--
    var rightnow = new Date();
    var weekday = rightnow.getDay();
    var themonth = rightnow.getMonth();
    var thedate = rightnow.getDate();
    var theyear = rightnow.getYear();
// set the Days of the week
    var someday = new Array(7);
    someday[0] = "Sunday";
    someday[1] = "Monday";
    someday[2] = "Tuesday";
    someday[3] = "Wednesday";
    someday[4] = "Thursday";
    someday[5] = "Friday";
    someday[6] = "Saturday";
// Set the Month Numbers to Be Recognizable
    themonth+=1;
// Set the Year Data for 4 digits
    if (theyear<2000)
        theyear += 1900;
//-->
</SCRIPT>
</head>
<body>

<H1>The Date:</H1>
<SCRIPT language="JavaScript">
<!--
    document.write(someday[weekday] + ", " +themonth+"/" + thedate + "/" + theyear);
//-->
</SCRIPT>

</body>
</html>


範例

Html Source Browser Display

The Date:

狀態列時鐘
<html>
<head>
<SCRIPT language="JavaScript">
<!--
function theclock()
{
    var rightnow = new Date();
    var thehours = rightnow.getHours();
    var themins = rightnow.getMinutes();
    var theseconds = rightnow.getSeconds();
// format the hours, minutes, and seconds with leading zeros
    if (thehours<10) thehours = "0" + thehours;
    if (themins<10) themins = "0" + themins;
    if (theseconds<10) theseconds = "0" + theseconds;
// write the time to the status bar
    window.status=thehours + ":" + themins + ":" + theseconds;
}
setInterval("theclock()", 1000);
//-->
</SCRIPT>
</head>
<body>
<H1>The Time is in the Status Bar at the Bottom!</H1>
</body>
</html>


範例

Html Source Browser Display

The Time is in the Status Bar at the Bottom!