如何找到剩余时间 - JavaScript?

How to find remaining time - JavaScript?

假设我的活动开始于

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 时,它​​正在工作。但我的日子很重要。

正在计算剩余时间:

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`);

您需要将天数添加到 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 天后时间的方法是使用 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)