I have a database of that I would like to sort based on the current date and time. Is there a way to call the current time in wix?
Hello Mike,
You can sort database by field (ascending or descending) and if you want to sort by date, easiest way would be to use: wix-dataset - Velo API Reference - Wix.com and sort in descending order by “_createdDate” or “_updatedDate” fields depending on your needs.
//This would show newly created records at the top
$w("#myDataset").setSort( wixData.sort()
.descending("_createdDate")
);
//This would show recently updated records at the top
$w("#myDataset").setSort( wixData.sort()
.descending("_updatedDate")
);
Is this what you were looking for?
Not quite, each item in the database represents an “event” and will have a date and time field or possibly a string with the date. What I need to display is the next three events to take place, for example, today is Jan 18th, it should display events on the 19th and 20th but not on the 17th. To do this I need to be able to find out the current date and time of the system.
Thank you for an example, now I see the whole picture.
In this case, I would suggest you to use : https://www.wix.com/code/reference/wix-dataset.html#setFilter functionality.
Let’s say in database your event’s date is represented in “date” field which is set to Date and Time type.
Then you would need to get date just like you would using javascript and put it as a variable in .setFilter method. To achieve functionality in your example you would have to use .gt filter:
import wixData from 'wix-data';
$w.onReady(function () {
//Get current date
let currentDate = new Date();
//Gat records with future dates compared to current date
$w("#dataset1").setFilter( wixData.filter()
.gt("date", currentDate)
);
});
You can also look for more filters to achieve any needed filtering functionality in the link above.
Hi, I get " TypeError: Date is not a constructor" message, when I use “let Now = new Date()”. Is it something else that is wrong?
What I have currently are below. Could you please help?
let Now = new Date();
let From = $w("#PickupAddress").value;
let To = $w("#DropoffAddress").value;
let Date = $w("#DepartDate").value;
let Time = $w("#DepartTime").value;
let data = {
"BookingDateTime" : Now,
"PickupAddress" : From,
"DropoffAddress" : To,
"DepartDate" : Date,
"DepartTime" : Time
};
console .log(data);
I think the “Date” is a Javascript reserved word in property “DepartDate”: Date
Also consider using dataset.onReady before setting filter.
$w(“#myDataset”).onReady( () => { console.log(“The dataset is ready”); } );
The dataset may not load fully when you call the setFilter function.