Home > Software engineering >  Check if an hour is between 2 time periods - React native
Check if an hour is between 2 time periods - React native

Time:02-23

I'm creating an Auto-Logout functionality (which needs to be logged out every day at 9 am) in my react-native app, so for that I came up with the logic:

So what I'm doing is, lets say I have my parent stack component, in that inside the useEffect, suppose the app is just opened, I will check if I have lastRunTime saved in async-storage, if true, then I will create a new Date object pointing today's current date, then if 9:00 am comes in between these 2 times, I will logout and set lastRunTime as Date.now().toString() in async-storage.

// auto-logout at 9 am everyday
  useEffect(() => {
    const lastRunTime = AsynStorage.getItem('lastRunTime');
    if (lastRunTime) {
      const justOpened = new Date();
      const lastOpened = new Date(lastRunTime);
      if (9 <= justOpened.getHours() && 9 >= lastOpened.getHours()) {
        logout();
        AsynStorage.setItem('lastRunTime', now.toString());
      }
    } else {
      console.log('9 am not in time range');
      AsynStorage.setItem('lastRunTime', new Date().toString());
    }
  }, []);

But it is not giving the correct result, always printing console.log('9 am not in time range'); even though I provided yesterday's date and Date.now().

const lastOpened = new Date(2022, 1, 21, 18, 0); // yesterday's 6 pm
const justOpened = new Date(); // right now

// now obviously today's 9 am was in between the 2 time intervals, but still it logs: console.log('9 am not in time range');

Can someone help me out here?

CodePudding user response:

Possible solutions

First of all, just to clarify, useEffect(() => {...}, []) only runs once, when you create the component. It can be considered as an equivalent of componentDidMount from older terminology.

So, if you want to log the user out at 9, if the user opens your app at 8:59 and waits until 9:01, they won't be logged out, because useEffect ran at 8:59. You could solve this issue with any kind of interval and periodically check if time is past 9:00.

The most ideal solution would be is to handle this logout feature backend side, if you have control over the backend, as that has most certainty that if backend is online and logic is correct, your user will be logged out.

The second most ideal solution would be is to have some kind of background task scheduled in your app, that makes sure that the user is logged out, regardless if your app is open.

The third most ideal solution is to use some kind of interval as I've explained above but that only works if the app is open.


Problem with your current code:

if (9 <= justOpened.getHours() && 9 >= lastOpened.getHours()) {

This if is incorrect, because let's say, lastOpened.getHours is 17 (I opened the app yesterday at 17:00), I won't be logged out, because 9 >= 17 is false. You could use some date handling library like date-fns or moment (less recommended because of not using plain Date objects) to make date handling easier, check if your current date is after today's 9:00 and if your lastRunTime is before your current date.

  • Related