<p>I think your problem is you're converting the date to a year string then converting that to an atom value. Just convert the date straight to an atom value:</p>

<pre><code>{% set yearStart = "#{year}-01-01"|atom %}
{% set yearEnd = "#{year}-12-31"|atom %}

{% set entries = craft.entries({
    section: "programme",
    isOngoing: 0,
    startDate: ["and", "&gt;= #{yearStart}", "&lt; #{yearEnd}"]
}).all %}
</code></pre>

CC BY-SA 4.0
2019-09-11T08:21:27.863Z
Thanks! I think the year vars aren't quite right though, getting this error:

DateTime::__construct(): Failed to parse time string (#{year}-01-01) at position 0 (#): Unexpected character
James Greig
CC BY-SA 4.0
2019-09-11T16:23:21.643Z
I don't usually use escaped variables, I was just mimicking [the docs](https://docs.craftcms.com/v3/date-time-fields.html#settings). Anyway glad you got it working.
kmgdev
CC BY-SA 4.0
2019-09-11T22:21:59.540Z
Oh apparently [interpolated variables only work inside double quotes](https://twig.symfony.com/doc/2.x/templates.html#string-interpolation). Updated the answer to match.
kmgdev
CC BY-SA 4.0
2019-09-11T00:43:08.850Z
2019-09-11T22:22:35.713Z
2019-09-11T22:22:35.713Z
kmgdev
<p><strong>EDIT: kmgdev's solution is now updated and marked correct! :)</strong></p>

<p>Fixed by slightly modifying kmgdev's solution slightly so that the year variables were interpolated correctly:</p>

<pre><code>{% set yearStart = (year ~ '-01-01')|atom %}
{% set yearEnd = (year ~ '-12-31')|atom %}

{% set query = {
  section: 'programme',
  isOngoing: 0,
  startDate: ( ['and', '&gt;=' ~ yearStart, '&lt;=' ~ yearEnd])
} %}

{% set programmeEntries = craft.entries(query).all() %}
</code></pre>

CC BY-SA 4.0
2019-09-11T08:59:13.650Z
2019-09-12T09:14:02.003Z
2019-09-12T09:14:02.003Z
James Greig
<p>So I'm trying to get all entries that have a particular year assigned to a custom date field. (Could be any day/month in that year).</p>

<p>The custom field is <code>startDate</code> and my attempt at a query - which doesn't work - is below...</p>

<p>PS. <code>year</code> is a 4 digit variable set via the last segment of the url and pulled into the template as a variable, and giving you <code>2018</code>, <code>2019</code> or whatever...</p>

<pre><code>{% set query = {
  section: 'programme',
  isOngoing: 0
} %}
{% set programmeEntries = craft.entries(query).search('startDate:' ~ year|date('Y-m-d')) %}
</code></pre>

<p>PS. I think it's more like this but I can't get the syntax right...</p>

<pre><code>{% set query = {
  section: 'programme',
  isOngoing: 0,
  startDate: ( ['and', '&gt;=' ~ (year~'-01-01')|date('Y')|atom, '&lt;=' ~ (year~'-01-01')|date('Y')|date_modify('+12 months')|atom])
} %}
</code></pre>

<datetime><query>
Query entries by custom date field (for a particular year)
# Query entries by custom date field (for a particular year)

(Asked by: username_0 on 2019-09-10)

So I'm trying to get all entries that have a particular year assigned to a custom date field. (Could be any day/month in that year).
The custom field is startDate and my attempt at a query - which doesn't work - is below...
PS. year is a 4 digit variable set via the last segment of the url and pulled into the template as a variable, and giving you 2018, 2019 or whatever...
{% set query = {
  section: 'programme',
  isOngoing: 0
} %}
{% set programmeEntries = craft.entries(query).search('startDate:' ~ year|date('Y-m-d')) %}

PS. I think it's more like this but I can't get the syntax right...
{% set query = {
  section: 'programme',
  isOngoing: 0,
  startDate: ( ['and', '>=' ~ (year~'-01-01')|date('Y')|atom, '<=' ~ (year~'-01-01')|date('Y')|date_modify('+12 months')|atom])
} %}

**Tags:** datetime, query


---

## 2 answers

### Answer by username_1 on 2019-09-11. Score: 5 | Accepted answer

I think your problem is you're converting the date to a year string then converting that to an atom value. Just convert the date straight to an atom value:
{% set yearStart = "#{year}-01-01"|atom %}
{% set yearEnd = "#{year}-12-31"|atom %}

{% set entries = craft.entries({
    section: "programme",
    isOngoing: 0,
    startDate: ["and", ">= #{yearStart}", "< #{yearEnd}"]
}).all %}

> **Comments:**
> **username_0**: Thanks! I think the year vars aren't quite right though, getting this error:  DateTime::__construct(): Failed to parse time string (#{year}-01-01) at position 0 (#): Unexpected character  
> **username_1**: I don't usually use escaped variables, I was just mimicking [the docs](https://docs.craftcms.com/v3/date-time-fields.html#settings). Anyway glad you got it working.  
> **username_1**: Oh apparently [interpolated variables only work inside double quotes](https://twig.symfony.com/doc/2.x/templates.html#string-interpolation). Updated the answer to match.  

---

### Answer by username_0 on 2019-09-11. Score: 2

EDIT: username_1's solution is now updated and marked correct! :)
Fixed by slightly modifying username_1's solution slightly so that the year variables were interpolated correctly:
{% set yearStart = (year ~ '-01-01')|atom %}
{% set yearEnd = (year ~ '-12-31')|atom %}

{% set query = {
  section: 'programme',
  isOngoing: 0,
  startDate: ( ['and', '>=' ~ yearStart, '<=' ~ yearEnd])
} %}

{% set programmeEntries = craft.entries(query).all() %}


---

