-1

I am fetching data from firebase i need to know how can i check difference between the fetch time and current time

Example

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

What i need if data.time - current time is less then or equal to 3 hours then i need to console the time left in 3 hours.

I have time in second and nanoseconds.

enter image description here

Let me clear more. For example data time is 2.30AM and current time is 3.30AM So its not completed 3 hours till now so i need time left which is 2 hours left. and if time completed like current time is 6.30 so simple console time exceed.

Working with this code but showing error 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));
}
UmaiZ
  • 93
  • 6
  • 18
  • your logic is not clear on what you are trying to do. lastLogin: is that a timestamp? or seconds since last login? either way, what javascript code are you using for current timestamp? also, it doesn't make sense to ask if a time-diff == 3 hours. you mean less than 3 hours? – Rick Feb 18 '20 at 20:33
  • yes time stamp - current time <= (less or equal to 3) if this situation match i need to see the difference when it will complete 3 hour – UmaiZ Feb 18 '20 at 20:35
  • If you want some sort of logout after a timeout service in Ionic I already provided a solution here: https://stackoverflow.com/questions/45816050/implementing-session-timeout-service-for-ionic-angular-with-timer-reset-with-ea Let me know if this helps. – JGFMK Feb 18 '20 at 20:47
  • You want to wait till 3 hours has elapsed. In which case the current time will always be greater than your timestamp. So the condition should be current timestamp - old time stamp. Or please correct me if I am understanding your question wrong. – ruth Feb 18 '20 at 20:47

2 Answers2

1

Your timestamp lastLogin.seconds is in Epoch time.

So subtract your current timestamp Math.floor(Date.now() / 1000) from your timestamp and check if it's less than 10800 seconds (3 hours). The following code should do

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

Note: the reverse conversion contains multiple division operators. Division operator usually incurs high latency. As a result, your result might be a second or two delayed than you expect.

ruth
  • 29,535
  • 4
  • 30
  • 57
  • thants but can we check how many hours or minute left to complete or elapsed? – UmaiZ Feb 18 '20 at 20:48
  • @UmaiZ you have to do math for that. – Brandon Dyer Feb 18 '20 at 20:59
  • @UmaiZ Yes you can, just convert the seconds to human readable format as shown in this answer: https://stackoverflow.com/a/1322798/6513921. I've updated my answer. – ruth Feb 18 '20 at 21:00
  • thanks. can you tell little more in this function secondsToHHMMSS all values seems like constant i need to pass my values or it will remain constant ? – UmaiZ Feb 18 '20 at 21:06
  • @UmaiZ: I forgot to remove the constant value from the answer I mentioned. The function should work now. You send in your seconds and it returns it in HH:MM:SS format. – ruth Feb 18 '20 at 21:23
  • thank you its working in online compiler i try to add my code in ionic its showing this error Type 'string' is not assignable to type 'number'. – UmaiZ Feb 18 '20 at 21:27
  • after this comment. // if you want strings with leading zeroes: – UmaiZ Feb 18 '20 at 21:29
  • I need to see more information. Please update the error throwing code in your question. – ruth Feb 18 '20 at 21:31
  • Yes i update in end also comment the code where the error is showing – UmaiZ Feb 18 '20 at 21:36
  • That I understood from your previous comment. Please show how you are calling this function in your code with ionic. – ruth Feb 18 '20 at 21:37
0

To answer the question you asked

Assuming this.data.time is in seconds, you can use Date.now() to get the current time in milliseconds, and divide that by 1000 to get seconds.

Since an hour is 3600 seconds, we can then compare our time difference with this multiplied by our desired hours.

const currentTime = Date.now() / 1000;

if(this.data.time - currentTime < 3 * 3600) {
  console.log("Please wait 'til difference")
}
Brandon Dyer
  • 1,316
  • 12
  • 21
  • As the above answer problem is in to see remaining time to complete 3 hours – UmaiZ Feb 18 '20 at 21:37
  • @UmaiZ: In that case why don't use another answer from the link I attached? See my answer, I've included one more function that doesn't use `padStart`. If it still throws error, problem is not in this function but in the value you are sending in. What value are you sending to function? Are you sure it is in seconds? – ruth Feb 19 '20 at 06:22