简体   繁体   English

使用 moment.js 倒计时到特定的日期和小时

[英]Using moment.js to countdown to a specific day and hour

I am using moment.js for providing time and dates of specific locations all around the world.我正在使用 moment.js 来提供世界各地特定地点的时间和日期。 Now I want to have a countdown timer where it is always counting down to the next monday AND a specific hour that monday.现在我想要一个倒数计时器,它总是倒计时到下一个星期一和那个星期一的特定小时。

I have already seen some solutions that are close (but no cigar), but the solutions usually have a specific date in mind.我已经看到了一些接近的解决方案(但没有雪茄),但这些解决方案通常有一个特定的日期。

I just want it to have an hourly countdown to the next upcoming monday at a specific hour.我只希望它在特定时间对下一个即将到来的星期一进行每小时倒计时。 So for example, the output can be "Time until opening: 2 days, 6 hours", which will be from the current local time that I already have from moment.js.例如,输出可以是“开放时间:2 天,6 小时”,这将来自我从 moment.js 已有的当前本地时间。

You could use the latest moment.js fromNow function.您可以使用最新的 moment.js fromNow函数。

Here is example if opening date is next monday, 10:00以下是开放日期为下周一 10:00 的示例

var openingDate = moment().day(1).hour(10).minute(0).second(0);
var fromNow = openingDate.fromNow();
console.log(fromNow);

will output in 19 hoursin 19 hours输出

You can use this code I personally use it.你可以使用我个人使用的这段代码。 You can change the number of seconds as needed, for example, on the day 12960000 seconds您可以根据需要更改秒数,例如当天 12960000 秒

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://momentjs.com/downloads/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="countdown"></div> <title>Static Template</title> </head> <body> <script> var countDownDate = moment().add(12960000, 'seconds'); var x = setInterval(function() { diff = countDownDate.diff(moment()); if (diff <= 0) { clearInterval(x); // If the count down is finished, write some text $('.countdown').text("EXPIRED"); } else $('.countdown').text(moment.utc(diff).format("HH:mm:ss")); }, 1000); </script> </body> </html>

enter code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM