Displaying a button during 14 days then hiding it

Hello everyone,

I have a repeater that is displaying data from a collection (newest content published). I created a red text box (saying that a content has just been haded, “new”) and I want it to appear for 2 weeks on each new content, then it has to disappear.
So, I was trying to get the created date from the dataset (automatically generated by Wix), add 14 days to it, and compare it to the current date. The box “New” has to appear until current date > created date+14d.

In order to do that, I first tried to see on other posts if someone had the same issue. I mixed up some solutions and ended up with somthing like this :

import wixData from 'wix-data';
export function box24_viewportEnter(event)  {
 function difference(dateOne, dateTwo) {
 let item = $w("#dataset3").getCurrentItem();
 let dateField = item._createdDate;
        dateField = dateField.toLocaleDateString();
 let startDate = (dateField);
 var startTimeStamp = (startDate).getTime();
 let endDate = new Date(dateTwo);
 var endTimeStamp = (endDate).getTime();
 var microSecondsDiff = Math.abs(startTimeStamp - endTimeStamp);
//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);

 if (daysDiff > 14) {
        $w('#box24').hide() ;
      } else {
            $w('#box24').show() ;
        }
    }
}

But as you can see, I am only a beginner with javascript, and this is still not working.

Here is the final result that I want (litlle red box on the top right corner) :

And I finally want to say that I am sorry for my quite bad level of english, i tried hard to describe clearly my problem without using any translator :wink:

Kind regards,
Clément

Hello everyone,

I tried a second time, but changing the whole code. I now try to compare two times in milliseconds.

Here is the new code :

import wixData from 'wix-data';
export function box24_viewportEnter(event) {
 var today = new Date();
 var timetoday = today.getTime();
 var publicationDate = new Date ($w("#dataset3").getCurrentItem()._createdDate);
 var timepublication = publicationDate.getTime();
 var timefourteendays = timepublication + (14 * 86400000);
 var fourteendays = new Date(timefourteendays);
    console.log(fourteendays);
 if (fourteendays > timetoday) {
        $w("#box24").show();
    } else {
        $w("#box24").hide();
    }
}

Obviously it still doesn’t work, but I think that the problem is that I use a wrong formula to get the ‘created date’ generated by Wix.

I want to mention that I tried to use the solution described in this post : https://www.wix.com/corvid/forum/community-discussion/30-days-between-todays-date-and-a-date-from-an-input-then-carry-out-a-function

Kind regards,
Clément