【问题标题】:How to find remaining time - JavaScript?如何找到剩余时间 - JavaScript?
【发布时间】:2021-10-26 15:44:29
【问题描述】:

假设我的活动开始于

publishDateTime: "2021-08-23T16:45:53.378"

它会在持续天数内结束,

duration: 3

如何求总剩余时间?

function timer(endTime = "2021-08-27T16:45:53.378") {
  const second = 1000,
    minute = second * 60,
    hour = minute * 60,
    day = hour * 24;

  let end = new Date(endTime).getTime(),
    x = setInterval(function () {
      let now = new Date().getTime(),
        timeLeft = end - now;

      let days = Math.floor(timeLeft / (day)),
        hours = Math.floor((timeLeft % (day)) / (hour)),
        minutes = Math.floor((timeLeft % (hour)) / (minute)),
        seconds = Math.floor((timeLeft % (minute)) / second);

      console.log('Time left:' + days + "Days " + hours + "Hrs " + minutes + "Mins " + seconds + "secs")

      if (timeLeft < 0) {
        console.log('Times up');
        clearInterval(x);
      }
    }, 1000)                    // time refresh in ms
}

timer("2021-08-23T16:45:53.378")

我正在尝试这样,但在这里我不知道如何将持续时间转换为时间戳或这种格式。但是当我尝试使用这种格式的 endTime 时,它​​正在工作。但我有天数。

【问题讨论】:

    标签: javascript time timestamp


    【解决方案1】:

    计算剩余时间:

    const publishDateTime = "2021-08-23T16:45:53.378";
    const duration = 3; // days
    
    const durationMs = duration * 24 * 3600 * 1000;
    const startDateTime = new Date(publishDateTime);
    const endDateTime = new Date(startDateTime.valueOf() + durationMs);
    const now = new Date();
    const remainingTimeMs = Math.max(0, Math.min(endDateTime.valueOf() - now.valueOf(), durationMs));
    console.log(`Remaining time: ${remainingTimeMs} ms`);

    【讨论】:

      【解决方案2】:

      您需要在endDate 中添加天数

      类似这样的:

      function timer(endTime, durationDay = 3) {
        const second = 1000,
          minute = second * 60,
          hour = minute * 60,
          day = hour * 24;
      
        let endDate = new Date(endTime);
        endDate.setDate(endDate.getDate()+durationDay);
        
        let end = endDate.getTime();
        let x = setInterval(function () {
            let now = new Date().getTime(),
              timeLeft = end - now;
      
            let days = Math.floor(timeLeft / (day)),
              hours = Math.floor((timeLeft % (day)) / (hour)),
              minutes = Math.floor((timeLeft % (hour)) / (minute)),
              seconds = Math.floor((timeLeft % (minute)) / second);
      
            console.log('Time left:' + days + "Days " + hours + "Hrs " + minutes + "Mins " + seconds + "secs")
      
            if (timeLeft < 0) {
              console.log('Times up');
              clearInterval(x);
            }
          }, 1000)                    // time refresh in ms
      }
      
      timer("2021-08-23T16:45:53.378")
      

      https://jsfiddle.net/ah57nypq/

      【讨论】:

        【解决方案3】:

        获取 3 天后时间的方法是使用 new date() 获取当前时间并在其上加上 3 天(根据小时数、分钟数等计算)。我已经修改了你的代码,让它像这样工作。

        function timer(endDays = 1) {
          const second = 1000,
            minute = second * 60,
            hour = minute * 60,
            day = hour * 24;
        
          let end = new Date().getTime() + endDays * day,
            x = setInterval(function () {
              let now = new Date().getTime(),
                timeLeft = end - now;
        
              let days = Math.floor(timeLeft / (day)),
                hours = Math.floor((timeLeft % (day)) / (hour)),
                minutes = Math.floor((timeLeft % (hour)) / (minute)),
                seconds = Math.floor((timeLeft % (minute)) / second);
        
              console.log('Time left:' + days + "Days " + hours + "Hrs " + minutes + "Mins " + seconds + "secs")
        
              if (timeLeft < 0) {
                console.log('Times up');
                clearInterval(x);
              }
            }, 1000)                    // time refresh in ms
        }
        
        timer(3)

        【讨论】:

        • 您的回答也很好,但是对于今天开始的某些事情,今天之后结束endDays。但是刷新页面或导航器失败了怎么办。计时器将重置。
        • 是的,你是对的。我误解了你的问题。唯一的修改是在计时器函数中添加一个 publishDateTime 参数并使用它来代替 new Date()。
        猜你喜欢
        • 2020-09-25
        • 1970-01-01
        • 2013-01-22
        • 2011-04-11
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多