简体   繁体   English

特定时间后从几天切换到几小时

[英]Switch from days to hours after a certain time

Goal: Once the time hits two days or less, then it switches to hours instead of days.目标:一旦时间达到两天或更短,就会切换到小时而不是几天。

Problem: In checking code by changing the date, it still only shows the days left, and not the hours.问题:在通过更改日期检查代码时,它仍然只显示剩余的天数,而不显示小时数。 Am I missing something?我错过了什么吗?

var countDownDate = new Date("Nov 15, 2020 11:59:59").getTime();

var x = setInterval(function() {

  var now = new Date().getTime();
    
  var distance = countDownDate - now;
    
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    
    
 if (distance < 2) {
    clearInterval(x);
    document.getElementById("aep-countdown-date").innerHTML = hours + " hours left";
  } 
    
  else if (distance < 0) {
    clearInterval(x);
    document.getElementById("aep-countdown-date").innerHTML = "The time has ended.";
  }
    
  else  {
      clearInterval(x);
      document.getElementById("aep-countdown-date").innerHTML = days + " days left";
  }
});

The Date method getTime returns: Date 方法getTime返回:

the number of milliseconds* since the Unix Epoch.自 Unix 纪元以来的毫秒数*。

Your code countDownDate - now returns the number of milliseconds from now until the target date.您的代码countDownDate - now返回从现在到目标日期的毫秒数。

However, your comparison distance < 2 is treating distance (which is in ms) as if it were in days.但是,您的比较distance < 2distance (以毫秒为单位)视为以天为单位。

You probably want你可能想要

ms in seconds ------------------+
seconds in hour ---------+      |
hours in day ------+     |      |
         days -+   |     |      |
               |   |     |      |
if (distance < 2 * 24 * 3600 * 1000) {
   /* ... */
}

With help of the previous two responses, I figured it out.在前两个回答的帮助下,我想通了。 I added "2x" to the hours variable and changed the variable to days in the if statements and rid of the setInterval because it wasn't needed (I only need days and hours, not milliseconds and for it to constantly be firing):我在 hours 变量中添加了“2x”,并将 if 语句中的变量更改为 days 并去掉了 setInterval,因为它不需要(我只需要几天和几小时,而不是毫秒,并且它会不断触发):

var countDownDate = new Date("Nov 15, 2020 11:59:59").getTime();

    $(function() {
        var now = new Date().getTime();

        var distance = countDownDate - now;

        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor(
            (2 * (distance % (1000 * 60 * 60 * 24))) / (1000 * 60 * 60)
        );

        document.getElementById("aep-countdown-date").innerHTML =
            days + " days left.";

        if (days < 0) {
            document.getElementById("aep-countdown-date").innerHTML = "Time has ended.";
        }

        if (days < 2) {
            document.getElementById("aep-countdown-date").innerHTML =
                hours + " hours left.";
        }
    });

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

相关问题 在一段时间后如何在ReactJS中从一个屏幕切换到另一个屏幕? - How to switch from one screen to another after a certain time in ReactJS? 如何从jQuery datepicker禁用特定时间后的周末和前几天的假期? - How to disable holidays with weekends and previous days after a certain time from jquery datepicker? 如何计算到达某一天的剩余时间(以天、小时、分钟和秒为单位)? - How to calculate the time left in days, hours, minutes, and seconds left to reach certain day? React Ant 设计为什么禁用今天后几天的某些小时和分钟? - React Ant design Why are certain hours and minutes of days after today disabled? 从javascript中的营业时间中减去天数和小时数 - subtract days and hours from business hours in javascript JavaScript计时器从特定时间倒数几个小时 - JavaScript timer to count down from a certain time for a number of hours 按分钟,按小时,按天等格式化时间 - Formating time by minutes, by hours, by days etc 随时间流逝改变日子和时间 - change days and hours by how time flies 检查特定日期的时间功能 - Time function that checks for certain days 从一组时间范围(休息)中减去后如何计算总小时数(工作时间) - How to calculate the total hours (working hours) after subtracting from a set of time ranges (break)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM