<p>It looks like I figured it out by doing this:</p>

<pre><code>.dateOrdered('and', '&gt;=' ~ startDate, '&lt;=' ~ endDate)
</code></pre>

CC BY-SA 3.0
2017-06-23T17:43:17.257Z
If you want to include orders that were completed _on_ the end date, you can do `endDate|date_modify('+1 day')` :)
carlcs
CC BY-SA 3.0
2017-06-23T17:54:45.987Z
Does it not take the time portion into account in the parameter? For end dates, I am passing strings like "2017-06-22 23:59:59".
Benjamin Kohl
CC BY-SA 3.0
2017-06-23T18:10:33.287Z
It does take the time into account, but only when you provide one – what you’re currently not doing! ;) `'>=' ~ startDate` returns a string ">=2017-06-02". You’d need to explicitly format the output using the `date` filter, e.g. `'>=' ~ startDate|date('c')` which would then output something like ">=2017-06-02T08:26+0800". If you prepend a `date_modify` filter you can do all sort of useful date and time calculations, see http://php.net/manual/en/datetime.formats.relative.php
carlcs
CC BY-SA 3.0
2017-06-23T18:15:40.837Z
Sorry @Ben, I just noticed that I assumed `startDate` is a DateTime object in your code, which may not be the case at all.
carlcs
CC BY-SA 3.0
2017-06-23T18:40:11.710Z
We are getting the expected results now so I think we're all set. Thanks for the help!
Benjamin Kohl
CC BY-SA 3.0
2017-06-23T17:38:43.607Z
2017-06-23T17:38:43.607Z
Benjamin Kohl
<p>I am attempting to get orders that were completed between two dates and the "before" and "after" criteria parameters don't seem to work. Perhaps I am using the wrong syntax? Example of how I am using them:</p>

<pre><code>.after('2017-06-02 00:00:00')
</code></pre>

<plugin-craftcommerce><datetime>
How do I query for orders that were completed between two dates?
# How do I query for orders that were completed between two dates?

(Asked by: username_0 on 2017-06-23)

I am attempting to get orders that were completed between two dates and the "before" and "after" criteria parameters don't seem to work. Perhaps I am using the wrong syntax? Example of how I am using them:
.after('2017-06-02 00:00:00')

**Tags:** plugin-craftcommerce, datetime


---

## 1 answers

### Answer by username_0 on 2017-06-23. Score: 2 | Accepted answer

It looks like I figured it out by doing this:
.dateOrdered('and', '>=' ~ startDate, '<=' ~ endDate)

> **Comments:**
> **username_1**: If you want to include orders that were completed _on_ the end date, you can do `endDate|date_modify('+1 day')` :)  
> **username_0**: Does it not take the time portion into account in the parameter? For end dates, I am passing strings like "2017-06-22 23:59:59".  
> **username_1**: It does take the time into account, but only when you provide one – what you’re currently not doing! ;) `'>=' ~ startDate` returns a string ">=2017-06-02". You’d need to explicitly format the output using the `date` filter, e.g. `'>=' ~ startDate|date('c')` which would then output something like ">=2017-06-02T08:26+0800". If you prepend a `date_modify` filter you can do all sort of useful date and time calculations, see http://php.net/manual/en/datetime.formats.relative.php  
> **username_1**: Sorry @Ben, I just noticed that I assumed `startDate` is a DateTime object in your code, which may not be the case at all.  
> **username_0**: We are getting the expected results now so I think we're all set. Thanks for the help!  

---

