Date.getUTCSeconds()方法是 JavaScript Date 对象原型的一部分。它用于根据协调世界时 (UTC) 检索日期对象的秒部分。返回值将是介于 (0 到 59) 之间的整数,用于指定日期的秒数。如果提供的 Date 对象“无效”,则此方法将返回 Not a Number (NaN) 作为结果。此外,此方法不接受任何参数。
UTC 代表协调世界时。它是世界调节时钟和时间的主要时间标准。而印度标准时间 (IST) 是在印度观察到的时间,IST 和 UTC 之间的时差为 UTC+5:30(即 5 小时 30 分钟)。
语法
以下是 JavaScript Date.getUTCSeconds() 方法的语法 -
getUTCSeconds();
此方法不接受任何参数。
返回值
返回值是一个整数,表示 UTC 时区中给定日期对象的秒部分。
示例 1
在下面的示例中,我们将演示 JavaScript Date.getUTCSeconds() 方法的基本用法 -
<html>
<body>
<script>
const currentDate = new Date();
const seconds = currentDate.getUTCSeconds();
document.write(seconds);
</script>
</body>
</html>
输出
它根据世界时返回日期的秒部分。
示例 2
在此示例中,我们从提供的日期检索并打印 seconds 组件 -
<html>
<body>
<script>
const specificDate = new Date("December 21, 2023 12:30:45 UTC");
const seconds = specificDate.getUTCSeconds();
document.write(seconds);
</script>
</body>
</html>
输出
上面的程序返回整数 45 作为秒值。
示例 3
以下示例根据 Universal time 每 2 秒打印一次日期的 seconds 组件。
<html>
<body>
<script>
function printSeconds() {
const currentDate = new Date();
const seconds = currentDate.getUTCSeconds();
document.write(seconds + "<br>");
}
setInterval(printSeconds, 2000);
</script>
</body>
</html>
输出
正如我们所看到的,秒数每 2 秒打印一次。