Getting the dates query to behave correctly