MyApp.Order
|> where([m], m.inserted_at > datetime_add(^Ecto.DateTime.utc, -2, "week"))
|> where([m], m.inserted_at < datetime_add(^Ecto.DateTime.utc, 0, "week"))
datetime_add(^Ecto.DateTime.utc, 0, "week") does not seem to make it as current week. So, this query fetches record from last week and current week.
Alternatively,
defp weekly_orders do
{interval1, interval2} = week_intervals
MyApp.Order
|> where([m], m.inserted_at >= datetime_add(^Ecto.DateTime.utc, ^interval1, "day"))
|> where([m], m.inserted_at < datetime_add(^Ecto.DateTime.utc, ^interval2, "day"))
end
defp week_intervals do
{-(day_of_the_week + 6) , -(day_of_the_week - 1)}
end
defp day_of_the_week do
:erlang.date
|> :calendar.day_of_the_week
end
This fails on Monday because datetime_add(Ecto.DateTime.utc, 0, "day")
- What does
datetime_add(Ecto.DateTime.utc, 0, "day")return or denote? - What's the right way to query for records of just last week using ecto?