【发布时间】: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