如何查询存储在Wix集合中的两个日期字段在两个日期之间?

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月7日 04:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定