【发布时间】:2018-01-05 14:52:48
【问题描述】:
我希望对特定日期和时间(2018 年 1 月 10 日 19:30)进行倒计时。这在很大程度上是我能够做到的。下面的代码显示了剩余的天数、小时数、分钟数和秒数。
棘手的一点是要获得特定的时间段。倒计时应响应以下内容: 1. 在截止日期和时间显示消息“现在上线”。也就是 2018 年 1 月 10 日 19:30。 2. 同一天,但在 19:30 之前,它应该说“今晚开始直播” 3.截止日期前的完整一天(从00:00到23:59)应该说“最后一天” 4. 在那之前的完整天数应该说'还有很多天'
我成功完成了第 1 步和第 2 步,但我无法获得截止日期前的完整天数和之前的完整天数。那是因为我无法在截止日期之前定义完整的一天(以及之前的几天)。因为它将“1 天”计为 1 月 10 日 19:30 之前的 1 天(因此它还考虑了 19:30 的小时/分钟)。
第 1 步和第 2 步我在 if 循环中进行了管理,但我不知道如何执行第 3 步和第 4 步。第 3 步应该说类似“计算一天,但在 2018 年 1 月 10 日 00:00 之前”。所以它应该减去 19:30 到 2018 年 1 月 9 日 00:00-23:59。第 4 步也是如此。有人可以修复我的代码吗?
// Get todays date and time
var now = new Date().getTime();
// Set the date we're counting down to
var countDownDate = new Date("Januari 10, 2018 19:30").getTime();
// Find the distance between now an 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);
// Display the result
this.timeleft.text = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
// countdown day 19:30
if ((days == 0) && (hours == 0) && (minutes == 0)) {
this.countdown.text = "NOW GOING LIVE!";
// countday day 00:00 - 19.30
} else if ((days == 0) && (hours <= 19) && (minutes <= 30)) {
this.countdown.text = "GOING LIVE TONIGHT!";
// 9 January 00:00 - 23:59
} else if ((days <= 1) && (hours >= 19) && (minutes >= 30)) {
this.countdown.text = "LAST DAY";
// days before 10 January
} else if (days >= 1) {
this.countdown.text = "MANY DAYS TO GO";
}
【问题讨论】:
-
例如,您可以使用 Date API 来确定@987654321@,而不是使用原始时间。另外,在单独的注释中,您有一个错字 - 它应该是 January 而不是 Januari。
-
这是要重复使用的吗?
标签: javascript