Math 與 Date 物件

Math 物件

屬性

E 屬性

範例: math_e.htm
<script language="JavaScript">
<!--
    window.alert(Math.E);
//-->
</script>
LN10 屬性
LN2 屬性
LOG10E 屬性
LOG2E 屬性
PI 屬性
SQRT2 屬性
SQRT1_2 屬性

方法

基本的方法

這些方法都只有一個參數, 而且回傳一個數值。
範例: 計算平方根sqrt.htm
<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>

兩個參數的方法

max()和min()方法
範例minmax.htm
<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>
pow()方法
pow()方法的第一個參數是基數, 第二個參數是次方。
範例: pow.htm
<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>
ceil()方法
ceil()方法傳回大於或等於所傳入參數的最小整數。
範例: ceil.htm
<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>
floor()方法
round()方法

random()方法

random()方法傳回一個介於 0 和 1 之間的隨機浮點數。
隨機整數
要得到一個隨機整數, 可以把 random()方法所得到的結果乘以一個整數, 再刪除小數部分。 例如:
    var rand_num = Math.random()*6;
    var rand_int = Math.floor(rand_num);
即可得到一個介於 0 和 5 之間的隨機整數。
隨機引文
範例:rndtxt.htm
<html>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<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>
隨機影像
範例rndimg.htm
<html>
<head>
<script language="JavaScript">
<!--
    var r_images = new Array(10);
    r_images[0] = "../../images/cards/10h.png";
    r_images[1] = "../../images/cards/11h.png";
    r_images[2] = "../../images/cards/12h.png";
    r_images[3] = "../../images/cards/13h.png";
    r_images[4] = "../../images/cards/4h.png";
    r_images[5] = "../../images/cards/5h.png";
    r_images[6] = "../../images/cards/6h.png";
    r_images[7] = "../../images/cards/7h.png";
    r_images[8] = "../../images/cards/8h.png";
    r_images[9] = "../../images/cards/9h.png";
    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>

Date 物件

Date 物件用於處理日期和時間。
   var rightnow = new Date();

屬性

constructor 屬性

prototype 屬性

方法

取值的方法

設值的方法

其他方法

  • toDateString()方法
    將日期轉換為更易讀的格式。

    含 Date 的 Script 程式

    使用Date()方法獲得當日的日期

    <html>
    <body>
    <script>
    var d=new Date();
    document.write(d);
    </script>
    </body>
    </html>
    

    看效果

    當日的日期顯示在網頁的適當位置

    <html>
    <head>
    <meta charset="big5">
    <title>今天的日期</title>
    </head>
    <body>
    <p id="demo">點擊按鈕獲取今天的日期和時間。</p>
    <button onclick="myFunction()">點我</button>
    <script>
    function myFunction(){
      var d = new Date();
      var x = document.getElementById("demo");
      x.innerHTML=d;
    }
    </script>
    </body>
    </html>
    

    看效果

    在網頁顯示時鐘

    <html>
    <head>
    <meta charset="big5">
    <title>時鐘</title>
    <script>
    function startTime(){
      var today=new Date();
      var h=today.getHours();
      var m=today.getMinutes();
      var s=today.getSeconds();  // 在小於10的數字前加一個‘0’
      m=checkTime(m);
      s=checkTime(s);
      document.getElementById('txt').innerHTML=h+":"+m+":"+s;
      t=setTimeout(function(){startTime()},500);
    }
    function checkTime(i){
      if (i<10){
      i="0" + i;
      }
      return i;
    }
    </script>
    </head>
    <body onload="startTime()">
    <div id="txt"></div>
    </body>
    </html>
    

    看效果

    在網頁寫日期

    範例 date.htm
    <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>
    
    
    

    狀態列時鐘

    範例statusbar.htm
    <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>
    

    參考資料