当用户点击“获取倒计时”按钮时,通过弹窗显示剩余时间(格式hh:mm:ss)
如下,望采纳
// 定义结束时间
const endDate = new Date("December 10, 2022 12:00:00");
// 获取当前时间
const currentDate = new Date();
// 计算剩余时间
const remainingTime = endDate - currentDate;
// 转换为格式化的时间字符串
const formattedTime = formatTime(remainingTime);
// 显示时间
alert(formattedTime);
// 定义formatTime()函数
function formatTime(time) {
const hours = Math.floor(time / 3600 / 1000);
const minutes = Math.floor(time / 60 / 1000) % 60;
const seconds = Math.floor(time / 1000) % 60;
// 将时间转换为hh:mm:ss格式
return `${hours}:${minutes}:${seconds}`;
}