JavaScript 日期设置方法


目录

    显示目录


设置日期方法允许您设置日期值(年、 日期对象的月、日、小时、分钟、秒、毫秒)。


设置日期方法

Set Date 方法用于设置日期的一部分:

setDate()

将日期设置为数字 (1-31)

setFullYear()

设置年份(可选月份和日期)

setHours()

设置小时(0-23)

setMilliseconds()

设置毫秒(0-999)

setMinutes()

设置分钟(0-59)

setMonth()

设置月份(0-11)

setSeconds()

设置秒(0-59)

setTime()

设置时间(自 1970 年 1 月 1 日起的毫秒数)


setFullYear() 方法

setFullYear() 方法设置日期对象的年份。在此示例中,到 2020 年:

例子

const d = new Date();
d.setFullYear(2020);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method sets the year of a date object:</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

setFullYear() 方法可以可选设置月份和日期:

例子

const d = new Date();
d.setFullYear(2020, 11, 3);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method can optionally set month and day.</p>
<p>Please note that month counts from 0. December is month 11:</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>


setMonth() 方法

setMonth() 方法设置日期对象的月份 (0-11):

例子

const d = new Date();
d.setMonth(11);

自己尝试一下→

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setMonth()</h2>
<p>The setMonth() method sets the mont of a date object.</p>
<p>Note that months count from 0. December is month 11:</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

setDate() 方法

setDate() 方法设置日期对象的日期 (1-31):

例子

const d = new Date();
d.setDate(15);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setDate()</h2>
<p>The setDate() method sets the day of a date object:</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

setDate() 方法还可用于为日期添加天数

例子

const d = new Date();
d.setDate(d.getDate() + 50);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setDate()</h2>
<p>The setDate() method can be used to add days to a date.</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

如果添加天数会改变月份或年份,则日期对象会自动处理更改。


setHours() 方法

setHours() 方法设置日期对象的小时 (0-23):

例子

const d = new Date();
d.setHours(22);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setHours()</h2>
<p>The setHours() method sets the hours of a date object:</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

setMinutes() 方法

setMinutes() 方法设置日期对象的分钟 (0-59):

例子

const d = new Date();
d.setMinutes(30);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setMinutes()</h2>
<p>The setMinutes() method sets the minutes of a date object (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

setSeconds() 方法

setSeconds() 方法设置日期对象的秒数 (0-59):

例子

const d = new Date();
d.setSeconds(30);

自己尝试一下 →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript setSeconds()</h2>
<p>The setSeconds() method sets the seconds of a date object (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

比较日期

可以轻松比较日期。

以下示例将今天的日期与 2100 年 1 月 14 日进行比较:

例子

let text = "";
const today = new Date();
const someday = new Date();
 someday.setFullYear(2100, 0, 14);
if (someday > today) {
  text = "Today is before January 14, 2100.";
} else {
    text = "Today is after January 14, 2100.";
}

自己尝试一下→

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
let text;
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
  text = "Today is before January 14, 2100.";
} else {
  text = "Today is after January 14, 2100.";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

JavaScript 从 0 到 11 计算月份。一月是 0。十二月是 11。

完整的 JavaScript 日期参考

如需完整的日期参考,请访问我们的:

完整的 JavaScript 日期参考。

该参考包含所有日期属性的描述和示例 方法。