0
const check=()=>{
   const cacheIntervalInMin = 1;
   const cacheExpiryTime = new Date();
   cacheExpiryTime.setMinutes(cacheExpiryTime.getMinutes() + cacheIntervalInMin);
 

   const lastRequest = new Date().toISOString();
   let a= new Date(lastRequest);
   if(a>cacheExpiryTime){ 
       console.log(Expired')
   }
   else console.log('Not expired');
}
check();
  

I want to set the expiry time to 30 mins but everytime I call this function it is always false . a never becomes greater than expiry time. How to solve this? How to achieve this?

Nithyashree B L
  • 157
  • 1
  • 2
  • 12
  • Just convert the lastRequest to new Date(lastRequest) after JSON.prase – Waqas Ahmed Jan 27 '22 at 10:13
  • I would suggest instead of using `JSON.stringify(new Date()))` to use `new Date().toISOString()` and then when you get the item from `AsyncStorage` parse it to a date object [like this](https://stackoverflow.com/questions/27012854/how-to-change-iso-date-string-to-date-object) – Kapobajza Jan 27 '22 at 10:21
  • 1
    Have a look at [momentjs](https://momentjs.com/), I always use this lib when dealing with date. You can find usefull fonctions such as : [is-after](https://momentjs.com/docs/#/query/is-after/) or [is-before](https://momentjs.com/docs/#/query/is-before/) – P-A Jan 27 '22 at 10:26
  • You create a new cache expiry time every time you check which is always 1 minute in hte future. You need to store this somewhere else – phuzi Jan 27 '22 at 11:11

0 Answers0