我有一个来自某人的工作代码,关于倒计时定时器,使用 Moment.JS --> [他的代码。https:/codepen.iojavaniguspenKrMRvd ] . 我试图复制它使用 Day.JS你可以说我想把它从 moment.js 迁移到 day.js。我成功地迁移了它,直到将目标(date+time)与当前(date+time)分开。 --> [我的代码。https:/codepen.iojustreadthispenbGVvvXP。 ]. 但现在我卡住了,当我想用1秒的时间来分馏我的最后一个代码部分(下一步将使它不断地分馏,对不对?)。我尝试 console.log(dayjs.preciseDiff(duration, interval, true));
间隔值,我设置为1秒,它会得到数据的NaN。 有没有人知道如何使它的工作原理?我试图不使用时刻.js,因为我想坚持使用day.js,如果可能的话。 再次编辑,我把JS文件放到了HTML中,以便于从这里看到,而不是从codepen。.
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs-plugin-utc"></script>
<script src="https://unpkg.com/[email protected]/precise-range.js"></script>
<time></time>
<script>
var eventTime, currentTime, interval, duration;
dayjs.extend(dayjsPluginUTC.default);
dayjs.extend(preciseDiff);
interval = 1;
eventTime = dayjs('2020-05-15T07:59:50+00:00').format();
// based on time set in user's computer time / OS
currentTime = dayjs.utc().format();
// get duration between two times
duration = dayjs.preciseDiff(eventTime, currentTime ,true);
console.log(dayjs.preciseDiff(duration, interval, true));
</script>
希望这对你有用。
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
</style>
</head>
<body>
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
</body>
</html>