JavaScript Date setMinutes() 方法
在 JavaScript 中,Date.setMinutes() 方法用于将 Date 对象的分钟值设置为指定的数值(介于 0 到 59 之间)。该方法允许我们在不更改日期和时间其他部分的情况下修改 Date 对象的分钟部分。 setMinutes() 方法不会创建新的 Date 对象,而是修改当前日期对象。
如果此方法提供的参数为"NaN",则日期将被设置为"无效日期",并返回"NaN"。
语法
以下是 JavaScript Date setMinutes() 方法的语法 -
setMinutes(minutesValue, secondsValue, msValue)
参数
此方法接受三个参数。下文将对此进行描述 -
- minutesValue - 一个表示分钟的整数(0 到 59 之间)。
- secondsValue(可选) - 一个表示秒的整数(0-59)。
- msValue(可选) - 一个表示毫秒的整数(0-999)。
返回值
返回 1970 年 1 月 1 日 00:00:00 UTC 与更新后的 Date 对象之间的毫秒数。
示例 1
在下面的示例中,我们使用 JavaScript Date.setMinutes() 方法将当前日期对象的分钟设置为"30"-
<html> <body> <script> let date = new Date(); document.write(date, "<br>"); date.setMinutes(30); document.write(date); </script> </body> </html>
输出
如果执行上述程序,我们可以看到"分钟"段的值已设置为 30。
示例 2
在下面的示例中,我们使用 getMinutes() 方法将当前时间加 15 分钟 -
<html> <body> <script> let date = new Date(); document.write(date, "<br>"); date.setMinutes(date.getMinutes() + 15); document.write(date.getMinutes()); </script> </body> </html>
输出
正如我们在输出中看到的,当前日期已添加 15 分钟。
示例 3
如果 setMinutes() 方法提供的参数为"NaN",则日期将被设置为"无效日期",并返回"NaN"作为结果 -
<html> <body> <script> let date = new Date(); date.setMinutes(NaN); document.write(date, "<br>") document.write(date.getMinutes()); </script> </body> </html>
输出
如果我们执行上述程序,它将返回 NaN 作为结果。

