<p>You're nearly there! The <code>and</code> query you’re building just needs to be declared as an array:</p>

<pre><code>{% set puppyAnimals = craft.entries({
    section: 'animals',
    orderBy: 'postDate',
    animalDateOfBirth: ['and',  "&lt;= #{now | atom}", "&gt;= #{now | date_modify('-1 year') | atom}"]
}).all()  %}
</code></pre>

<p>The brackets around the value provided for <code>animalDateOfBirth</code> was being interpreted as a single string, followed by a missing key.</p>

<p>(I also opted for Twig’s interpolation syntax to make the series of array elements a little clearer, and used the <code>atom</code> filter to produce fully-qualified dates)</p>

<blockquote>
  <p>One more note: I switched the greater-than and less-than operators, as I suspect the query would have returned no results—(after today, but earlier than last year)! </p>
</blockquote>

CC BY-SA 4.0
2019-01-28T18:21:17.327Z
Thanks for the help! This is definitely not the first time the array syntax has got me.
Matthew Rotter
CC BY-SA 4.0
2019-01-28T18:22:42.700Z
Follow up question: just trying to do {% set puppyAnimals = posts({}) tells me that "posts" isn't a function, but calling posts.search({}) give me Undefined index: query. Any idea?
Matthew Rotter
CC BY-SA 4.0
2019-01-29T21:58:31.867Z
I'm sorry! Tapped this out on my phone hastily, and totally flubbed the actual syntax. Updated! You can perform the entire query in a single statement, as above… no need to do it in multiple calls. Good to note that the query this builds can't be used as a function, but it can have the initial properties set (via `{…}`), or by chaining method calls on it (the Craft docs use this "dot notation", like `craft.entries().myFieldName('myValue')`)
August Miller
CC BY-SA 4.0
2019-01-29T22:24:49.783Z
For anyone else stumbling upon this, be sure and check out the [Element Query](https://docs.craftcms.com/v3/dev/element-queries/) documentation for some examples!
August Miller
CC BY-SA 4.0
2019-01-30T03:40:21.207Z
All good! That's exactly when I ended up doing {% set puppyPosts = posts.animalDateOfBirth(['and',  "<= #{now | date('c')}", ">= #{now | date_modify('-1 year') | date('c')}"]).all %}
Matthew Rotter
CC BY-SA 4.0
2019-01-28T17:45:08.450Z
2019-01-29T21:59:29.523Z
2019-01-29T21:59:29.523Z
August Miller
<p>Trying to get entries between two dates and am getting: 'A hash key must be followed by a colon (:). Unexpected token "operator" of value "~" ("punctuation" expected with value ":")' </p>

<pre><code>{% set posts = craft.entries({
    section: animal,
    orderBy: "postDate" })  %}


{% set puppyAnimals = posts({
    animalDateOfBirth: 'and',  "&gt;= " ~ now|date , '&lt;=' ~ now|date_modify('-1 year')|date })  %}
</code></pre>

<p>It's Craft 3 so I may just have the syntax wrong.</p>

<entries><craft3>
A hash key must be followed by a colon (:) syntax error
# A hash key must be followed by a colon (:) syntax error

(Asked by: username_0 on 2019-01-28)

Trying to get entries between two dates and am getting: 'A hash key must be followed by a colon (:). Unexpected token "operator" of value "~" ("punctuation" expected with value ":")' 
{% set posts = craft.entries({
    section: animal,
    orderBy: "postDate" })  %}


{% set puppyAnimals = posts({
    animalDateOfBirth: 'and',  ">= " ~ now|date , '<=' ~ now|date_modify('-1 year')|date })  %}

It's Craft 3 so I may just have the syntax wrong.

**Tags:** entries, craft3


---

## 1 answers

### Answer by username_1 on 2019-01-28. Score: 3

You're nearly there! The and query you’re building just needs to be declared as an array:
{% set puppyAnimals = craft.entries({
    section: 'animals',
    orderBy: 'postDate',
    animalDateOfBirth: ['and',  "<= #{now | atom}", ">= #{now | date_modify('-1 year') | atom}"]
}).all()  %}

The brackets around the value provided for animalDateOfBirth was being interpreted as a single string, followed by a missing key.
(I also opted for Twig’s interpolation syntax to make the series of array elements a little clearer, and used the atom filter to produce fully-qualified dates)

One more note: I switched the greater-than and less-than operators, as I suspect the query would have returned no results—(after today, but earlier than last year)!

> **Comments:**
> **username_0**: Thanks for the help! This is definitely not the first time the array syntax has got me.  
> **username_0**: Follow up question: just trying to do {% set puppyAnimals = posts({}) tells me that "posts" isn't a function, but calling posts.search({}) give me Undefined index: query. Any idea?  
> **username_1**: I'm sorry! Tapped this out on my phone hastily, and totally flubbed the actual syntax. Updated! You can perform the entire query in a single statement, as above… no need to do it in multiple calls. Good to note that the query this builds can't be used as a function, but it can have the initial properties set (via `{…}`), or by chaining method calls on it (the Craft docs use this "dot notation", like `craft.entries().myFieldName('myValue')`)  
> **username_1**: For anyone else stumbling upon this, be sure and check out the [Element Query](https://docs.craftcms.com/v3/dev/element-queries/) documentation for some examples!  
> **username_0**: All good! That's exactly when I ended up doing {% set puppyPosts = posts.animalDateOfBirth(['and',  "<= #{now | date('c')}", ">= #{now | date_modify('-1 year') | date('c')}"]).all %}  

---

