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

时间:2018-01-06 09:12:48

标签: javascript 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();

但它没有返回任何数据,因为它比较了日期和时间。每个客户的时间价值。我怎样才能按日期比较它们?

1 个答案:

答案 0 :(得分:1)

确保客户的BirthDate保存在DateTimeOffset类型的列中。

即使没有必要"时间"在您的场景中,您需要" 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()表示没有时间的BirthDate,即使它已保存在具有DateTimeOffset类型的列中。

请注意,您已将查询中的当前系统时间(此示例中为08:06:05.000Z)提交给服务器,而在服务器端,我们会根据以下内容确定正确日期时区偏移计算。 如果没有这个,您的解决方案就会不完整,特别适用于全球客户遍布全球的分布式应用程序。

当我们转换' date" 2017-09-05T08:06:05.000Z"'对于没有时间的2017-09-05,我们准备比较两个有效次。