Javascript 简明教程
JavaScript - Set Date Methods
Set Date Methods
JavaScript Set Date 方法是链接到 JavaScript 中 Date 对象的功能,旨在简化日期和时间结构中特定元素的调整和修改。这些方法使开发人员能够高效地更新 Date 对象中的各个组件,例如年份、月份、日期、小时和分钟,提供了一种在 JavaScript 应用程序中处理和操纵日期相关值的便捷方法。
此处,我们将详细讨论这些 JavaScript 设定日期的方法。下表包含最常用的设定日期的方法及其相应的描述。
Method |
Description |
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) |
接受输入为四位数的年份,并调整协调世界时或 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) 中的日期,可能更改秒、分钟、小时、日期、月份和年份来确保有效的日期和时间。 |
Examples
Example 1: Simple implementation of set methods
我们使用 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>
Example 2: Combining Set Date Methods for a Complex Update
复杂的日期操作结合了多个 set 方法:例如,通过增加两年来调整日期;减去一个月,然后增加 15 天。最后,精确且准确地将时间设置为 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>