When fetching data from firebase, I want to know how to calculate the difference between the fetch time and the current time.
For example:
if(this.data.time - current time <= (less or equal to) 3 hours) {
console.log('Please wait till difference')
}
If the difference between data.time and current time is less than or equal to 3 hours, I want to log the time left within 3 hours.
The timestamps are in seconds and nanoseconds.
https://i.sstatic.net/MUflU.png
Let me provide further clarification. For instance, if the data time is 2:30 AM and the current time is 3:30 AM, it hasn't been 3 hours yet so I need to display 2 hours remaining. If the current time reaches 6:30 AM, then simply log that the time has exceeded 3 hours.
I'm having issues with this code showing an error "Type 'string' is not assignable to type 'number'."
secondsToHHMMSS(totalSeconds: any): string { // taken 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;
// for strings with leading zeroes:
minutes = String(minutes).padStart(2, "0"); // encountering an error of String
hours = String(hours).padStart(2, "0"); // encountering an error of String
seconds = String(seconds).padStart(2, "0"); // encountering an error of String
return (hours + ":" + minutes + ":" + seconds);
}
const timeDiff = Math.floor(Date.now() / 1000) - lastLogin.seconds;
if (timeDiff <= 10800) {
console.log("3 hours haven't elapsed yet.");
console.log("Time remaining: " + this.secondsToHHMMSS(timeDiff));
}