Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T08:21:27.863Z
Answers.Text: 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
Answers.UserDisplayName: James Greig
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T16:23:21.643Z
Answers.Text: 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.
Answers.UserDisplayName: kmgdev
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T22:21:59.540Z
Answers.Text: 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.
Answers.UserDisplayName: kmgdev
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T00:43:08.850Z
Answers.LastActivityDate: 2019-09-11T22:22:35.713Z
Answers.LastEditDate: 2019-09-11T22:22:35.713Z
Answers.UserDisplayName: kmgdev
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T08:59:13.650Z
Answers.LastActivityDate: 2019-09-12T09:14:02.003Z
Answers.LastEditDate: 2019-09-12T09:14:02.003Z
Answers.UserDisplayName: James Greig
Body: <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>
  
Tags: <datetime><query>
Title: Query entries by custom date field (for a particular year)
ThreadText: # 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() %}
  
  ```
  
  
  ---
  
  