Items in a collection only for 24 hours

Hello,I would like to make sure that there is only one item in the collection for 24 hours, then it will be deleted. Is it possible?Thanks

Almost anything is possible using wix code. Ideas to help do this would be you would need to query the database and get the items “last updated” value. then have the code compare the time difference between the last updated value and the current time and if it is 24hrs or more user the dataset item remove to delete it.

I can’t give you code examples as i have never tried to do this but hopefully that will at least give you a starting idea.

Hello, thanks for the idea, but if i have more items? How can i take the date value from them? And also i saw that the value is in a different format.

Have a look at this forum post and the replies from Ohad (Wix Mod): https://www.wix.com/code/home/forum/community-discussion/pop-up-lightbox-everyday-once

Try taking the code for the lightbox popup and changing it so that instead of it getting the date of the popup, it instead gets the date when the item in the collection was added.

import wixWindow from 'wix-window';
import {local} from 'wix-storage';

$w.onReady(function () {
    let today = new Date().toDateString();
    let popupDate = local.getItem('popupDate');

    if( popupDate !== today) {
        wixWindow.openLightbox("<LightboxName>"); //change to the actual lightbox name
        local.setItem('popupDate', today);
    }
});

Then integrate the code to calculate the difference in date by using the next code batch from Ohad.

function parseDate(str) {
var mdy = str.split('/')
return new Date(mdy[2], mdy[0]-1, mdy[1])
}
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24))
}
datediff(parseDate(popupDate), parseDate(today))

You can also do time difference like this from Yisrael (Wix Mod):

let startDate = new Date("2018-12-03T00:00:00Z");
var now = new Date();
var startTimeStamp = (startDate).getTime();
var nowTimeStamp = now.getTime();
var microSecondsDiff = Math.abs(startTimeStamp - nowTimeStamp);
console.log("msecs diff: " + microSecondsDiff);
// Number of milliseconds in a day:
// 24 hrs/day * 60 minutes/hour * 60 seconds/minute * 1000 msecs/second
var daysDiff = Math.floor(microSecondsDiff / (1000 * 60 * 60 * 24));
console.log("days diff: " + daysDiff);
var minsDiff = Math.floor(microSecondsDiff / (1000 * 60));
console.log("minutes diff: " + minsDiff);

Then once that is all done, then you can add the remove function for the dataset:

Somebody has another solution?