JavaScript 中获取当前时间并精确到毫秒的方法是使用 Date
对象的 getTime()
方法或者 Date.now()
方法。
// 使用 Date 对象的 getTime 方法
let currentTimeMillis = new Date().getTime();
console.log("Current time in milliseconds (Date.getTime()):", currentTimeMillis);
// 使用 Date.now 方法
let currentTimeMillisNow = Date.now();
console.log("Current time in milliseconds (Date.now()):", currentTimeMillisNow);
Date
对象默认使用本地时区。Date.UTC()
。// 获取当前 UTC 时间戳
let currentUTCTimeMillis = Date.UTC(
new Date().getUTCFullYear(),
new Date().getUTCMonth(),
new Date().getUTCDate(),
new Date().getUTCHours(),
new Date().getUTCMinutes(),
new Date().getUTCSeconds(),
new Date().getUTCMilliseconds()
);
console.log("Current UTC time in milliseconds:", currentUTCTimeMillis);
通过上述方法,你可以准确地获取到当前时间的毫秒数,并根据需要进行相应的处理和应用。
没有搜到相关的文章