Database Time ago function

I’m currently using a repeater to display a number of items from a database, but wonder if there is a possibility to display: “Item published 5 minutes ago” for example. I know this can be hard since this is originally made in php, but wonder if there is a genius who could solve this for me in Wix Code. Thankful for an answer,

Karl

Each item (row) in a database collection has a time stamp.


By subtracting the item’s time stamp from the current time, you can display “item published x minutes ago” in the repeater items. You can set Repeater items using the onItemReady() and forEachItem() functions.

Hi Yisrael,
I’m a little unsure on how to do this. I know that there is a timestamp for every item in a database, and that the timestamp format is like the javascript Date format. But I would be super grateful if you could show me a couple lines of code on how to implement this.

Here’s a snippet for calculating the time difference:

// let's pretend we got the date from a database query
let dateFromQuery = new Date("2018-12-03T00:00:00Z");

// get the current time
var now = new Date();

// calculate the difference
var queryTimeStamp = (dateFromQuery).getTime();
var nowTimeStamp = now.getTime();
var microSecondsDiff = Math.abs(queryTimeStamp - nowTimeStamp);
console.log("msecs diff: " + microSecondsDiff);
// Number of milliseconds in a day:
//   24 hrs/day * 60 mins/hr * 60 secs/min * 1000 msecs/sec
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);

You can get more information on Dates and Times from the Javascript Date Reference.

Good luck

A million thanks @yisrael-wix for helping me out. Problem solved!

Hi @karlsellergren ,

I am trying the same thing on a repeater, but still can’t get it to work. Would you mind sharing how this was accomplished.

@yisrael-wix Can I get some help with this, I can’t seem to get this to work.

The calculation itself is in the Top Comment above - nothing to add there. What you need to do is use that calculation and set the appropriate field in each repeater item using the onItemReady() or forEachItemI() functions.

So, I am having an issue in getting the date from the query, to be clear I run the query on the dataset onready as well as the calculation? Then use the onitemready to set the field. Any way I can see an example of how this was done for a repeater? My bad, I am still learning code.

See the code examples in the onItemReady() API .

Hi @yisrael-wix !

I’m trying to achieve the same as the OP, I tried your code, and I couldn’t get it to work.

Here’s my current code :

$w("#repeater2").onItemReady(($w, itemData, index) => {

let postedDate = itemData._createdDate;

let dateFromQuery = new Date(postedDate);
// get the current time
var now = new Date();
// calculate the difference
var queryTimeStamp = (dateFromQuery).getTime();
var nowTimeStamp = now.getTime();
var microSecondsDiff = Math.abs(queryTimeStamp - nowTimeStamp);
console.log("msecs diff: " + microSecondsDiff);
// Number of milliseconds in a day:
//   24 hrs/day * 60 mins/hr * 60 secs/min * 1000 msecs/sec
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);

$w("#text89").text =
now()

});

Thanks for the help!