JavaScript - 設定日期方法



設定日期方法

JavaScript 設定日期方法是與 JavaScript 中的 Date 物件關聯的功能,旨在簡化日期和時間結構中特定元素的調整和修改。這些方法使開發人員能夠有效地更新 Date 物件中的各個元件,例如年份、月份、日期、小時和分鐘,為處理和操作 JavaScript 應用程式中的日期相關值提供了一種便捷的方法。

在這裡,我們將詳細討論這些 JavaScript 設定日期方法。下表包含最常用的設定日期方法及其對應的描述。

方法 描述
setFullYear(year) 設定日期物件的年份。接受四位數的年份。調整日期;如果它是閏年並且日期是 2 月 29 日,則保持不變;否則,它將更改為新年中最近的有效日期。
setMonth(month) 設定日期物件的月份 (0-11)。接受數值 (0-11)。調整日期,如果月份值超出有效範圍,則更改年份。
setDate(day) 設定日期物件中月份的日期 (1-31)。接受數值 (1-31)。調整日期,如果日期值超出當前月份的有效範圍,則更改月份和年份。
setHours(hours) 該函式設定日期物件的小時,範圍為 0-23,僅接受數字值。它根據需要調整日期以保持有效性,除了時間之外,還可能更改月份和年份。
setMinutes(minutes) 接受 0 到 59 之間的數字,並設定該特定日期物件的分鐘數。它以這樣一種方式調整日期,即小時、日期、月份和年份都是有效的日期和時間。
setSeconds(seconds) 設定日期物件的秒數 (0-59)。接受數值 (0-59)。調整日期,可能會更改分鐘、小時、日期、月份和年份以確保日期和時間有效。
setMilliseconds(ms) 設定日期物件的毫秒數 (0-999)。接受數值 (0-999)。調整日期,可能會更改秒、分、時、日、月和年以確保日期和時間有效。
setTime(milliseconds) 該函式設定自 1970 年 1 月 1 日以來的日期和時間(以毫秒為單位);它接受一個數值。然後,整個日期物件將轉換為反映提供的毫秒值。
setUTCFullYear(year) 以 4 位數的年份作為輸入,並調整協調世界時或 UTC,同時考慮閏年。
setUTCMonth(month) 設定日期物件的 UTC 月份 (0-11)。接受數值 (0-11)。調整協調世界時 (UTC) 中的日期,如果月份值超出有效範圍,則可能會更改年份。
setUTCDate(day) 接受 1 到 31 之間的數值,並設定該特定日期物件的 UTC 月份的日期。它基本上調整協調世界時 (UTC) 中的日期,如果日期值在當前月份的有效範圍內,則更改月份和年份。
setUTCHours(hours) 設定日期物件的 UTC 小時 (0-23)。接受數值 (0-23)。調整協調世界時 (UTC) 中的日期,可能會更改日期、月份和年份以確保日期和時間有效。
setUTCMinutes(minutes)

設定日期物件的 UTC 分鐘(0-59)。接受數值(0-59)。調整協調世界時 (UTC) 的日期,可能會更改小時、日期、月份和年份以確保日期和時間有效。
setUTCSeconds(seconds) 設定日期物件的 UTC 秒(0-59)。接受數值(0-59)。調整協調世界時 (UTC) 的日期,可能會更改分鐘、小時、日期、月份和年份以確保日期和時間有效。
setUTCMilliseconds(ms) 設定日期物件的 UTC 毫秒(0-999)。接受數值(0-999)。調整協調世界時 (UTC) 的日期,可能會更改秒、分鐘、小時、日期、月份和年份以確保日期和時間有效。

示例

示例 1:set 方法的簡單實現

我們使用 set 方法修改各種日期元件,從而展示每種方法的通用性。這些對當前日期的調整適應各種場景;它們不僅包括新增年份、月份和日期,還包括小時 - 甚至分鐘和毫秒。

<!DOCTYPE html>
<html>
<body>
   <div id="result">  
      <p id="setFullYear"></p>
      <p id="setMonth"></p>
      <p id="setDate"></p>
      <p id="setHours"></p>
      <p id="setMinutes"></p>
      <p id="setSeconds"></p>
      <p id="setMilliseconds"></p>
      <p id="setTime"></p>
      <p id="setUTCFullYear"></p>
      <p id="setUTCMonth"></p>
      <p id="setUTCDate"></p>
      <p id="setUTCHours"></p>
      <p id="setUTCMinutes"></p>
      <p id="setUTCSeconds"></p>
      <p id="setUTCMilliseconds"></p>
   </div>
   <script>
      const currentDate = new Date();
      currentDate.setFullYear(currentDate.getFullYear() + 1);
      document.getElementById("setFullYear").innerText = `setFullYear: ${currentDate.toDateString()}`;
     
      currentDate.setMonth(currentDate.getMonth() + 2);
      document.getElementById("setMonth").innerText = `setMonth: ${currentDate.toDateString()}`;
      
		currentDate.setDate(currentDate.getDate() + 5);
      document.getElementById("setDate").innerText = `setDate: ${currentDate.toDateString()}`;
 
      currentDate.setHours(currentDate.getHours() + 3);
      document.getElementById("setHours").innerText = `setHours: ${currentDate.toDateString()}`;
  
      currentDate.setMinutes(currentDate.getMinutes() + 15);
      document.getElementById("setMinutes").innerText = `setMinutes: ${currentDate.toDateString()}`;
      
		currentDate.setSeconds(currentDate.getSeconds() + 30);
      document.getElementById("setSeconds").innerText = `setSeconds: ${currentDate.toDateString()}`;
  
      currentDate.setMilliseconds(currentDate.getMilliseconds() + 500);
      document.getElementById("setMilliseconds").innerText = `setMilliseconds: ${currentDate.toDateString()}`;
      
		currentDate.setTime(currentDate.getTime() + 86400000); // 86400000 milliseconds in a day
      document.getElementById("setTime").innerText = `setTime: ${currentDate.toDateString()}`;
  
      currentDate.setUTCFullYear(currentDate.getUTCFullYear() + 1);
      document.getElementById("setUTCFullYear").innerText = `setUTCFullYear: ${currentDate.toDateString()}`;
  
      currentDate.setUTCMonth(currentDate.getUTCMonth() + 2);
      document.getElementById("setUTCMonth").innerText = `setUTCMonth: ${currentDate.toDateString()}`;
      
		currentDate.setUTCDate(currentDate.getUTCDate() + 5);
      document.getElementById("setUTCDate").innerText = `setUTCDate: ${currentDate.toDateString()}`;
       
		currentDate.setUTCHours(currentDate.getUTCHours() + 3);
      document.getElementById("setUTCHours").innerText = `setUTCHours: ${currentDate.toDateString()}`;
  
      currentDate.setUTCMinutes(currentDate.getUTCMinutes() + 15);
      document.getElementById("setUTCMinutes").innerText = `setUTCMinutes: ${currentDate.toDateString()}`;
  
      currentDate.setUTCSeconds(currentDate.getUTCSeconds() + 30);
      document.getElementById("setUTCSeconds").innerText = `setUTCSeconds: ${currentDate.toDateString()}`;
  
      currentDate.setUTCMilliseconds(currentDate.getUTCMilliseconds() + 500);
      document.getElementById("setUTCMilliseconds").innerText = `setUTCMilliseconds: ${currentDate.toDateString()}`;
   </script>
</body>
</html>

示例 2:結合 Set Date 方法進行復雜更新

複雜的日期操作結合了多個 set 方法:例如,它透過新增兩年調整日期;減去一個月,然後新增十五天。最後,以精確和準確的方式將時間設定為 18:30:45。

<!DOCTYPE html>
<html>
<body>
   <div id="result">
      <h2>Complex Date Manipulation</h2>
      <p id="complexManipulation"></p>
   </div>
   <script>
      const currentDate = new Date();

     // Combining multiple set methods for a complex update
     currentDate.setFullYear(currentDate.getFullYear() + 2);
     currentDate.setMonth(currentDate.getMonth() - 1);
     currentDate.setDate(currentDate.getDate() + 15);
     currentDate.setHours(18);
     currentDate.setMinutes(30);
     currentDate.setSeconds(45);

     document.getElementById("complexManipulation").innerText = 
	  `Complex Manipulation Result: ${currentDate.toDateString()} ${currentDate.toTimeString()}`;
   </script>
</body>
</html>
廣告