在设置里的代码处,页脚处的位置添加代码即可。
<div id="timer"></div>
<script>
function startTimer(fromDate, element) {
function update() {
const now = new Date();
let diff = Math.floor((now - fromDate) / 1000); // 秒数差
const year = 365 * 24 * 3600;
const month = 30 * 24 * 3600;
const day = 24 * 3600;
const hour = 3600;
const minute = 60;
const y = Math.floor(diff / year);
diff %= year;
const m = Math.floor(diff / month);
diff %= month;
const d = Math.floor(diff / day);
diff %= day;
const h = Math.floor(diff / hour);
diff %= hour;
const min = Math.floor(diff / minute);
const s = diff % minute;
let str = '感谢陪伴: ';
if (y > 0) str += y + '年';
if (m > 0 || y > 0) str += m + '月';
if (d > 0 || m > 0 || y > 0) str += d + '日';
if (h > 0 || d > 0 || m > 0 || y > 0) str += h + '时';
str += min + '分' + s + '秒';
element.textContent = str;
}
update();
setInterval(update, 1000);
}
const from = new Date('2021-09-11T00:00:00');
const el = document.getElementById('timer');
startTimer(from, el);
</script>