在 Ecto 中比较日期的正确方法是什么
What is the right way to compare dates in Ecto
比较范围内日期的正确方法是什么,使用减号运算符并比较日期是否大于 Ecto?
def has_valid_date_range(query) do
from ct in query,
where: (ct.end_date - from_now(0, "day")) > 0,
where: (ct.end_date - from_now(0, "day")) <= ct.due_notice
end
此查询的结果应该 return end_date 减今天大于 0 且 end_date 减今天小于 due_notice[=12 的所有行=]
但它 return 是我的错误
** (Ecto.Query.CompileError) ct.end_date() - from_now(0, "day") is not a valid query expression.
正如我们在评论部分了解到的,您想要 select end_date
在当前时间之后和从当前时间起 due_notice
天之前的记录。为此,您可以使用此查询:
where: ct.end_date > from_now(0, "day") and
ct.end_date <= datetime_add(ct.end_date, ct.due_notice, "day")
比较范围内日期的正确方法是什么,使用减号运算符并比较日期是否大于 Ecto?
def has_valid_date_range(query) do
from ct in query,
where: (ct.end_date - from_now(0, "day")) > 0,
where: (ct.end_date - from_now(0, "day")) <= ct.due_notice
end
此查询的结果应该 return end_date 减今天大于 0 且 end_date 减今天小于 due_notice[=12 的所有行=]
但它 return 是我的错误
** (Ecto.Query.CompileError) ct.end_date() - from_now(0, "day") is not a valid query expression.
正如我们在评论部分了解到的,您想要 select end_date
在当前时间之后和从当前时间起 due_notice
天之前的记录。为此,您可以使用此查询:
where: ct.end_date > from_now(0, "day") and
ct.end_date <= datetime_add(ct.end_date, ct.due_notice, "day")