英文:
How to query two date fields, stored in a Wix collection, between two dates?
问题
以下是您提供的代码的翻译部分:
我正在尝试通过使用一个查询函数来缩小Wix中的搜索结果,该函数使用两个日期字段。
数据以MM/DD/YYYY的格式存储在集合中。
在Wix中,似乎应该首先重新格式化从“datapicker”返回的值。我尝试过,但是没有成功。
你能帮助我解决这个代码问题吗?
我的代码是:
function search() {
let startingDatePickerValue = $w("#startingDatePicker").value;
let endingDatePickerValue = $w("#endingDatePicker").value;
wixData.query("Collection1")
.contains("title", $w('#input11').value)
.find()
.then(results => {
$w('#repeater1').data = results.items;
let searchResults = results.items;
if (startingDatePickerValue !== null || endingDatePickerValue !== null) {
let yearValue = startingDatePickerValue.getFullYear();
let monthValue = startingDatePickerValue.getMonth();
let dayValue = startingDatePickerValue.getDate();
let dateValue1 = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
let yearValue2 = endingDatePickerValue.getFullYear();
let monthValue2 = endingDatePickerValue.getMonth();
let dayValue2 = endingDatePickerValue.getDate();
let dateValue2 = new Date(yearValue2, monthValue2, dayValue2, 23, 59, 59);
let newQuery = results.query;
newQuery
.ge("startingDate", dateValue1)
.le("endingDate", dateValue2)
.find()
.then((newQueryResults) => {
$w('#repeater1').data = newQueryResults.items;
let newQuery = results.query;
console.log(dateValue1, dateValue2)
return newQuery
})
}
}
dataValue1和dataValue2的返回值为:
Tue Jan 10 2023 00:00:00 GMT+0n00 (GMT+0n:00) Sat Jan 14 2023 23:59:59 GMT+0n00 (GMT+0n:00)
英文:
I'm trying to narrow down my search results by using a query function that uses two date fields in Wix.
Data are stored with the format MM/DD/YYYY in the collection.
Strangely, in Wix, it seems one should first reformat a value returned from a "datapicker". I tried to do so, but it was in vein.
Could you please help me to the problem of code.
My code is:
function search() {
let startingDatePickerValue = $w("#startingDatePicker").value;
let endingDatePickerValue = $w("#endingDatePicker").value;
wixData.query("Collection1")
.contains("title", $w('#input11').value)
.find()
.then(results => {
$w('#repeater1').data = results.items;
let searchResults = results.items;
if (startingDatePickerValue !== null || endingDatePickerValue !== null) {
let yearValue = startingDatePickerValue.getFullYear();
let monthValue = startingDatePickerValue.getMonth();
let dayValue = startingDatePickerValue.getDate();
let dateValue1 = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
let yearValue2 = endingDatePickerValue.getFullYear();
let monthValue2 = endingDatePickerValue.getMonth();
let dayValue2 = endingDatePickerValue.getDate();
let dateValue2 = new Date(yearValue2, monthValue2, dayValue2, 23, 59, 59);
let newQuery = results.query;
newQuery
.ge("startingDate", dateValue1)
.le("endingDate", dateValue2)
.find()
.then((newQueryResults) => {
$w('#repeater1').data = newQueryResults.items;
let newQuery = results.query;
console.log(dateValue1, dateValue2)
return newQuery
})
}
}
the returned dataVlue1, dataValue2 values are
Tue Jan 10 2023 00:00:00 GMT+0n00 (GMT+0n:00) Sat Jan 14 2023 23:59:59 GMT+0n00 (GMT+0n:00)
答案1
得分: 0
只更改集合中的日期为带有时间的日期可能是最简单的方法。有一个用于包括时间的复选框。这将允许您使用JavaScript日期对象进行比较。
英文:
It might be easiest to just change the dates in the collection to dates with times. There is a checkbox for including the time. That will let you do the comparison with JavaScript Date objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论