JavaScript 中的星期幾數字表示?


在本文中,我們將學習如何在 JavaScript 中使用適當的示例將星期幾表示為數字。

為了獲取星期幾的數字表示,JavaScript 提供了 `getDay()` 方法。`getDay()` 是 Date 物件的一個方法。`getDay()` 方法返回 0 到 6 之間的數值。例如,星期日 = 0,星期一 = 1,星期二 = 2,星期三 = 3,以此類推。

為了更好地理解,讓我們深入瞭解 JavaScript 中 `getDay()` 方法的語法和用法。

語法

`getDay()` 方法的語法如下:

dateObject.getDay();

其中,`dateObject` 是由日期建立的物件。此方法返回 0 到 6 之間的數字(0 – 星期日,1 – 星期一,2 – 星期二,3 – 星期三,4 – 星期四,5 – 星期五,6 – 星期六)。

示例 1

這是一個示例程式,演示如何使用 `getDay()` 方法在 JavaScript 中獲取當前日期的星期幾數字表示。

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      const date = new Date(); //Get the day of the week of current date
      const day = date.getDay();
      document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day;
   </script>
</body>
</html>

執行上述程式碼後,將生成以下輸出。

示例 2

這是一個示例程式,演示如何使用 `getDay()` 方法在 JavaScript 中獲取使用者指定日期的星期幾數字表示。

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      const date = new Date("January 4 2001 04:04:04"); //Get the day of the week of particular date mentioned by the user
      const day = date.getDay();
      document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day;
   </script>
</body>
</html>

執行上述程式碼後,將生成以下輸出。

示例 3

這是一個示例程式,定義一個基於 Zeller 演算法的使用者自定義函式,用於在 JavaScript 中獲取星期幾的數字表示。

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      // Without using Date() and getDay()
      var dd,mm,yyyy;
      function calculateDay(dd, mm, yyyy) {
         const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
         const indexes_of_weekday = [0,1,2,3,4,5,6];
         if (mm< 3) {
            mm += 12;
            yyyy--;
         }
         var h = (dd + parseInt(((mm + 1) * 26) / 10) + yyyy + parseInt(yyyy / 4) + 6 * parseInt(yyyy / 100) + parseInt(yyyy / 400) - 1) % 7;
         return indexes_of_weekday[h];
      }
      var day = calculateDay(23,06,2022);
      document.getElementById("output").innerHTML = 'The day is : ' + day;
   </script>
</body>
</html>

執行上述程式碼後,將生成以下輸出。

更新於:2022-12-09

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.