-1

How can I check the yesterday date without doing one by one validation. I am storing data in session and try to fetch the data if current date is changed, (with date, month and year). I can do this by checking one by one but is their any simple way to do this.

Process is like this :

  1. user open the website (An api call make to the server and get the data and saved that data into localstorage)

  2. If user refresh the website all is working fine, bcoz I can fetch the data from localhost

  3. But from the admin side I add new data, now If user open the website it will get the old data not the new one,

So I am trying to check if 24 hours has been passed then a new API call make to the server and get the fresh data.

It will reduce the api calls in a sinlge daya as well as make the website more faster.

Pooja
  • 543
  • 7
  • 26
  • Have you considered the `websocket` way? – Giovanni Esposito Sep 20 '21 at 13:08
  • No, I am using timestamp to save data format, But it's gets changed in every second – Pooja Sep 20 '21 at 13:09
  • There are many questions on Stack Overflow about how to get yesterday's date in JavaScript, or to compare two dates. Have you searched for/looked at any of those? – Heretic Monkey Sep 20 '21 at 13:12
  • why don't compare with "timestamp / 24 / 60 / 60 / 1000" (date at 00:00:00.000, integer division, in javascript you have to use Math.trunc to eliminate the decimal places). Warning, check if you have to do some extra stuff on the day when time change because the daily saving time – Manuel Romeiro Sep 20 '21 at 13:36

1 Answers1

1

You could compare now's date to 24 hours ago using the javascript Date object

var yourDate = new Date('02.08.2021 05:31');
var timeStamp = Math.round(new Date().getTime() / 1000);
var yesterdayTimeStamp = timeStamp - (24 * 3600);
var isLessThen24Hours = yourDate >= new Date(yesterdayTimeStamp * 1000).getTime();

console.log("is less then 24 hours = " + isLessThen24Hours);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53