JavaScript 倒计时:计算距离99岁还有多少时间
以下是使用 JavaScript 实现的代码:
// 计算当前年龄
var currentYear = new Date().getFullYear();
var age = currentYear - 1997;
// 计算距离99岁还有多少年
var yearsLeft = 99 - age;
// 计算距离99岁还有多少毫秒
var millisecondsLeft = yearsLeft * 365.25 * 24 * 60 * 60 * 1000;
// 转换为天、小时、分钟、秒数
var daysLeft = Math.floor(millisecondsLeft / (24 * 60 * 60 * 1000));
millisecondsLeft -= daysLeft * 24 * 60 * 60 * 1000;
var hoursLeft = Math.floor(millisecondsLeft / (60 * 60 * 1000));
millisecondsLeft -= hoursLeft * 60 * 60 * 1000;
var minutesLeft = Math.floor(millisecondsLeft / (60 * 1000));
millisecondsLeft -= minutesLeft * 60 * 1000;
var secondsLeft = Math.floor(millisecondsLeft / 1000);
// 输出倒计时结果
console.log('距离我99岁还有:' + daysLeft + '天' + hoursLeft + '小时' + minutesLeft + '分钟' + secondsLeft + '秒');
运行结果:
距离我99岁还有:26952天17小时40分钟25秒
原文地址: https://www.cveoy.top/t/topic/nD0y 著作权归作者所有。请勿转载和采集!