如何仅按日期过滤数据而不比较它们在位框架中的时间?

How can I filter data only by date without comparing their times in bit framework?

我有以下代码:

// 2017-Sep(09)-05 is selected in date picker

const dateToSearch = new Date(2017, 8 /*Month starts from zero*/, 5);

// context is oData context.

const customersOfThatSpecificDay = await context.customers.filter((c, d) => c.BirthDate == day, { day: dateToSearch }).toArray();

但它 returns 没有数据,因为它正在比较每个客户的日期和时间值。我怎样才能只按日期比较它们?

确保客户的出生日期保存在 DateTimeOffset 类型的列中。

即使在您的场景中不需要 "time" 而您只需要 "date",您也必须使用 DateTimeOffset,因为时区的副作用。

// 2017-Sep(09)-05 is selected in date picker

const dateToSearch = new Date(2017, 8 /*Month starts from zero*/, 5, new Date().getHours(), new Date().getMinutes(), new Date().getSeconds()).toISOString();

// dateToSearch variable which is produced from your date picker must have two important things: 1- It should be in ISO format. 2- It's hour/minute/second should be the client's current date/time's hour/minute/second.

const customersOfThatSpecificDay = await context.customers.filter((c, d) => c.BirthDate.date() == d, { d: `date"${dateToSearch}"` }).toArray();

// This results into following odata query: (date(BirthDate) eq 'date"2017-09-05T08:06:05.000Z"')

c.BirthDate.date() 表示没有时间的出生日期,即使它保存在 DateTimeOffset 类型的列中也是如此。

请注意,您必须将查询中的当前系统时间(本例中为 08:06:05.000Z)提交给服务器,在服务器端,我们确定正确的日期 基于时区偏移计算。 如果没有这个,您的解决方案在某种程度上是不完整的,特别是对于全球分布的应用程序,其客户遍布世界各地。

当我们在没有时间的情况下将 'date"2017-09-05T08:06:05.000Z"' 转换为类似 2017-09-05 的内容时,我们准备比较两个 valid 时间。