【问题标题】:Angular how to subtract time from current timeAngular如何从当前时间中减去时间
【发布时间】:2020-06-02 22:40:45
【问题描述】:

我正在从 firebase 获取数据,我需要知道如何检查获取时间和当前时间之间的差异

例子

if(this.data.time - current time <= (less or equal) 3 hours) {
  console.log('Please wait till difference')
}

如果 data.time - 当前时间小于或等于 3 小时我需要什么,那么我需要控制 3 小时内剩余的时间。

我有以秒和纳秒为单位的时间。

让我再清楚一点。例如,数据时间是 2.30AM,当前时间是 3.30AM,所以到现在还没有完成 3 小时,所以我需要剩下 2 小时的时间。如果像当前时间一样完成的时间是 6.30,那么简单的控制台时间就会超过。

使用此代码但显示错误 Type 'string' is not assignable to type 'number'。

secondsToHHMMSS(totalSeconds: any): string { // from https://stackoverflow.com/a/1322798/6513921
  let hours = Math.floor(totalSeconds / 3600);
  totalSeconds %= 3600;
  let minutes = Math.floor(totalSeconds / 60);
  let seconds = totalSeconds % 60;

  // if you want strings with leading zeroes:
  minutes = String(minutes).padStart(2, "0"); //here showing error of String
  hours = String(hours).padStart(2, "0"); //here showing error of String
  seconds = String(seconds).padStart(2, "0"); //here showing error of String

  return (hours + ":" + minutes + ":" + seconds); 
}

const timeDiff = Math.floor(Date.now() / 1000) - lastLogin.seconds;
if (timeDiff <= 10800) {
  console.log('3 hours hasn\'t elapsed yet.');
  console.log('Time remaining: ' + this.secondsToHHMMSS(timeDiff));
}

【问题讨论】:

  • 您的逻辑不清楚您要做什么。 lastLogin:那是时间戳吗?或自上次登录以来的秒数?无论哪种方式,您为当前时间戳使用什么 javascript 代码?此外,询问时间差异是否 == 3 小时也没有意义。你的意思是不到 3 小时?
  • 是时间戳 - 当前时间
  • 如果你想在 Ionic 超时服务后进行某种注销,我已经在这里提供了解决方案:stackoverflow.com/questions/45816050/… 如果这有帮助,请告诉我。
  • 您想等到 3 小时过去。在这种情况下,当前时间将始终大于您的时间戳。所以条件应该是当前时间戳 - 旧时间戳。或者,如果我对您的问题的理解有误,请纠正我。

标签: angular typescript ionic-framework


【解决方案1】:

您的时间戳lastLogin.seconds 位于Epoch time

因此,从您的时间戳中减去您当前的时间戳Math.floor(Date.now() / 1000),并检查它是否小于 10800 秒(3 小时)。下面的代码应该做

secondsToHHMMSS(totalSeconds: any): string { // https://stackoverflow.com/a/1322798/6513921
  let hours = Math.floor(totalSeconds / 3600);
  totalSeconds %= 3600;
  let minutes = Math.floor(totalSeconds / 60);
  let seconds = totalSeconds % 60;

  // if you want strings with leading zeroes:
  minutes = String(minutes).padStart(2, "0");
  hours = String(hours).padStart(2, "0");
  seconds = String(seconds).padStart(2, "0");

  return (hours + ":" + minutes + ":" + seconds); 
}

secondsToHHMMSSRegex(seconds: any): any { // https://stackoverflow.com/a/17781037/6513921
    const date = new Date(1970,0,1);
    date.setSeconds(seconds);
    return date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
}

const timeDiff = Math.floor(Date.now() / 1000) - lastLogin.seconds;
if (timeDiff <= 10800) {
  console.log('3 hours hasn\'t elapsed yet.');
  console.log('Time remaining: ' + this.secondsToHHMMSSRegex(timeDiff));
}

注意:反向转换包含多个除法运算符。除法运算符通常会产生高延迟。因此,您的结果可能会比您预期的延迟一两秒。

【讨论】:

  • 不重要,但我们可以检查还剩多少小时或分钟来完成或经过?
  • @UmaiZ 你必须为此做数学。
  • @UmaiZ 是的,您可以,只需将秒数转换为人类可读的格式,如以下答案所示:stackoverflow.com/a/1322798/6513921。我已经更新了我的答案。
  • 谢谢。你能在这个函数中多说一点 secondsToHHMMSS 所有值似乎都是常数我需要传递我的值,否则它将保持不变?
  • @UmaiZ:我忘了从我提到的答案中删除常量值。该功能现在应该可以工作了。您在几秒钟内发送,它会以 HH:MM:SS 格式返回。
【解决方案2】:

回答你提出的问题

假设this.data.time 以秒为单位,您可以使用Date.now() 获取当前时间(以毫秒为单位),然后将其除以 1000 以获取秒数。

由于一小时是 3600 秒,因此我们可以将我们的时差与这个乘以我们想要的小时数进行比较。

const currentTime = Date.now() / 1000;

if(this.data.time - currentTime < 3 * 3600) {
  console.log("Please wait 'til difference")
}

【讨论】:

  • 如上答题在看剩余时间完成3小时
  • @UmaiZ:在这种情况下,为什么不使用我附加的链接中的另一个答案?看我的回答,我已经包含了一个不使用padStart 的函数。如果它仍然抛出错误,问题不在于这个函数,而在于你发送的值。你发送给函数的值是什么?你确定它在几秒钟内?
猜你喜欢
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2023-03-15
相关资源
最近更新 更多