JavaScript Date getUTCMilliseconds() 方法
JavaScript Date.getUTCMilliseconds() 方法用于根据 UTC 获取日期对象的毫秒值。此方法返回一个数值,该数值表示指定日期和时间的 UTC 毫秒数(介于 0 到 999 之间)。如果我们未在日期对象中指定毫秒部分,则此方法默认为"0"。
UTC 代表协调世界时。它是世界各国规范时钟和时间的主要时间标准。
语法
以下是 JavaScript Date getUTCMilliseconds() 方法的语法 -
getUTCMilliseconds();
此方法不接受任何参数。
返回值
一个数字,表示指定日期和时间的 UTC 毫秒数(介于 0 到 999 之间)。
示例 1
在下面的示例中,我们将演示 JavaScript Date getUTCMilliseconds() 方法的基本用法 -
<html> <body> <script> const currentDate = new Date(); const utcmilliseconds = currentDate.getUTCMilliseconds(); document.write(utcmilliseconds); </script> </body> </html>
输出
上述程序根据 UTC 返回日期的毫秒数。
示例 2
在此示例中,我们从指定的日期值获取毫秒值 -
<html>
<body>
<script>
const specificDate = new Date('2023-10-26 12:30:45.154');
const utcmilliseconds = specificDate.getUTCMilliseconds();
document.write(utcmilliseconds);
</script>
</body>
</html>
输出
上述程序返回整数 154 作为毫秒值。
示例 3
如果我们未在提供的日期中指定毫秒部分,它将返回"0"作为默认值 -
<html>
<body>
<script>
const specificDate = new Date('2023-10-26 12:30:45');
const utcmilliseconds = specificDate.getUTCMilliseconds();
document.write(utcmilliseconds);
</script>
</body>
</html>
输出
如我们所见,输出返回"0"。
示例 4
如果 Date 对象无效,此方法将返回"NaN"作为结果 -
<html>
<body>
<script>
const specificDate = new Date('sagtrjdh');
const utcmilliseconds = specificDate.getUTCMilliseconds();
document.write(utcmilliseconds);
</script>
</body>
</html>
输出
如我们所见,输出返回的是"NaN"。

