Javascript 简明教程

JavaScript - Set Date Methods

Set Date Methods

JavaScript Set Date 方法是链接到 JavaScript 中 Date 对象的功能,旨在简化日期和时间结构中特定元素的调整和修改。这些方法使开发人员能够高效地更新 Date 对象中的各个组件,例如年份、月份、日期、小时和分钟,提供了一种在 JavaScript 应用程序中处理和操纵日期相关值的便捷方法。

JavaScript Set Date Methods are functionalities linked to the Date object in JavaScript, designed to streamline the adjustment and modification of specific elements within a date and time structure. These methods empower developers to efficiently update individual components, such as year, month, day, hour, and minute, in a Date object, offering a convenient approach for handling and manipulating date-related values in JavaScript applications.

此处,我们将详细讨论这些 JavaScript 设定日期的方法。下表包含最常用的设定日期的方法及其相应的描述。

Here, we will discuss about these JavaScript set date methods in detail. The table below comprises of the most commonly used set date methods and their corresponding description.

Method

Description

setFullYear(year)

Sets the year of the date object. Accepts a four-digit year. Adjusts the date; if it’s a leap year and the date is February 29, it remains; otherwise, it changes to the closest valid date in the new year.

setMonth(month)

Sets the month of the date object (0-11). Accepts a numeric value (0-11). Adjusts the date, changing the year if the month value is outside the valid range.

setDate(day)

Sets the day of the month for the date object (1-31). Accepts a numeric value (1-31). Adjusts the date, changing the month and year if the day value is outside the valid range for the current month.

setHours(hours)

The function sets the hour of a date object within the range of 0-23, accepting only numeric values. It adjusts the date as necessary to maintain validity, potentially altering the month and year in addition to time.

setMinutes(minutes)

Accepts numbers ranging from 0 to 59 and sets the minutes of that particular date object. It adjusts the date in such a way that hour, date, month and year are a valid date and time.

setSeconds(seconds)

Sets the seconds of the date object (0-59). Accepts a numeric value (0-59). Adjusts the date, potentially changing the minute, hour, date, month, and year to ensure a valid date and time.

setMilliseconds(ms)

Sets the milliseconds of the date object (0-999). Accepts a numeric value (0-999). Adjusts the date, potentially changing the second, minute, hour, date, month, and year to ensure a valid date and time.

setTime(milliseconds)

The function sets the date and time in milliseconds since January 1, 1970; it accepts a numeric value. Then, the entire date object is transformed to reflect the provided milliseconds value.

setUTCFullYear(year)

Takes input as a 4 digit year and adjusts the Coordinated Universal Time or UTC, considering the leap years.

setUTCMonth(month)

Sets the UTC month of the date object (0-11). Accepts a numeric value (0-11). Adjusts the date in Coordinated Universal Time (UTC), potentially changing the year if the month value is outside the valid range.

setUTCDate(day)

Accepts numeric value between 1 to 31 and sets the UTC day of the month for that particular date object. It basically adjusts the date in Coordinated Universal Time (UTC), changing the month and year if the day value is in the valid range for the current month.

setUTCHours(hours)

Sets the UTC hour of the date object (0-23). Accepts a numeric value (0-23). Adjusts the date in Coordinated Universal Time (UTC), potentially changing the date, month, and year to ensure a valid date and time.

setUTCMinutes(minutes)

Sets the UTC minutes of the date object (0-59). Accepts a numeric value (0-59). Adjusts the date in Coordinated Universal Time (UTC), potentially changing the hour, date, month, and year to ensure a valid date and time.

setUTCSeconds(seconds)

Sets the UTC seconds of the date object (0-59). Accepts a numeric value (0-59). Adjusts the date in Coordinated Universal Time (UTC), potentially changing the minute, hour, date, month, and year to ensure a valid date and time.

setUTCMilliseconds(ms)

Sets the UTC milliseconds of the date object (0-999). Accepts a numeric value (0-999). Adjusts the date in Coordinated Universal Time (UTC), potentially changing the second, minute, hour, date, month, and year to ensure a valid date and time.

Examples

Example 1: Simple implementation of set methods

我们使用 set 方法来修改各种日期组件,从而展示每种方法中固有的多功能性。对当前日期的这些调整适应了不同的场景;它们不仅包括增加年份、月份和日期,还包括小时——甚至分钟和毫秒。

We employ set methods to modify a variety of date components, thereby demonstrating the versatility inherent in each method. These adjustments to the current date accommodate diverse scenarios; they include not only adding years, months and days but also hours - even minutes and milliseconds.

<!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>

Example 2: Combining Set Date Methods for a Complex Update

复杂的日期操作结合了多个 set 方法:例如,通过增加两年来调整日期;减去一个月,然后增加 15 天。最后,精确且准确地将时间设置为 18:30:45。

A sophisticated date manipulation combines multiple set methods: for instance, it adjusts the date by adding two years; subtracts one month then adds fifteen days. Finally with precision and accuracy, the time is set at 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>