Im trying to make function that checks if timestamp from database row (current_timestamp() -> for example: 2022-11-25 21:41:03) is 24 hours or more ago, thats a piece of code I have:
async function clearChannels() {
const deleted_tos = 0;
const response = await axios.get(`${apiurl}/get/all`);
JSON.parse(JSON.stringify(response.data)).forEach(function(element) {
const timestamp = element.CTS;
});
}
I just cant figure out how to make if statement that checks if timestamp const is 24 hours ago or more from now
CodePudding user response:
const timestamp = new Date(element.CTS) // milliseconds
const now = new Date() // milliseconds
const isPast24hrs = now - timestamp > (60 * 60 * 24 * 1000) // millisecond value
if(isPast24hrs) {
// your logic goes here
}
Hope it helped