Answers.Body: <p>Try this:</p>
  
  <pre><code>{% set entriesInYear = craft.entries
      .section('agenda')
      .limit(null)
      . dateAndTime([
          'and',
          '&gt;=' ~ year,
          '&lt;' ~ (year + 1)
      ])
      .all()
  %}
  </code></pre>
  
  <p>The <a href="https://docs.craftcms.com/api/v3/craft-elements-db-entryquery.html#method-before" rel="nofollow noreferrer"><code>.before()</code></a> and <a href="https://docs.craftcms.com/api/v3/craft-elements-db-entryquery.html#method-after" rel="nofollow noreferrer"><code>.after()</code></a> methods are convenience methods that hide the complexity of the date query on Craft's <code>postDate</code> field. They don't affect any other custom date fields like your <code>dateAndTime</code> field.</p>
  
  <p>The array for your <code>dateAndTime</code> field condition (in the above example) is more of a Yii Framework query thing where the optional <code>'and'</code> operator in the first position means all of the following conditions must apply. If omitted, it's assumed to be <code>'or'</code>.</p>
  
  <p>If you prefer, the array can also be expressed as a string of comma-separated conditions like so:</p>
  
  <pre><code>.dateAndTime('and, &gt;= ' ~ year ~ ', &lt; ' ~ (year + 1))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-25T15:27:21.843Z
Answers.LastActivityDate: 2019-01-25T15:32:31.910Z
Answers.LastEditDate: 2019-01-25T15:32:31.910Z
Answers.UserDisplayName: Sam Hernandez
Body: <p>I'm trying to set up a past events archive. Followed the guide provided by PT: <a href="https://craftcms.com/guides/creating-an-archive-page-for-entries" rel="nofollow noreferrer">craft cms guide to set up an archive</a>.</p>
  
  <p>The only difference is that I use a custom date field (dateAndTime) and not postDate. The route leads to the correct template.</p>
  
  <p><strong>The issue</strong></p>
  
  <p>When it loads the archives/agenda/2018 template (f. ex.), it displays all 2018 events (grouped by month) but also all 2019 events that where entered in Craft in 2018. This under the correct month, but obviously the wrong year.
  When it load the 2019 template, only the 2019 events that where entered in 2019 show up.
  I can't figure out where the code is wrong.</p>
  
  <p><strong>Route</strong></p>
  
  <p>url: archives/agenda/year (token)
  template: _sections/archives/agenda/index</p>
  
  <p><strong>Nav</strong></p>
  
  <pre><code>{% set firstEntryEver = craft.entries.section('agenda').orderBy('dateAndTime asc').one() %}
      &lt;ul&gt;
      {% for year in now|date('Y') .. firstEntryEver.dateAndTime|date('Y') %}
          &lt;li&gt;&lt;a href="archives/agenda/{{ year }}"&gt;{{ year }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Index</strong></p>
  
  <pre><code>    &lt;h1&gt;{{ year }}&lt;/h1&gt;
  
      {% set entriesInYear = craft.entries.section('agenda').limit(null).after(year).before(year+1).all() %}
      {% for month, entries in entriesInYear | group("dateAndTime|date('F')") %}
          &lt;h2&gt;{{ month }}&lt;/h2&gt;
          &lt;ul&gt;
              {% for entry in entries %}
                  &lt;li&gt;{{ entry.getLink() }}&lt;/li&gt;
              {% endfor %}
          &lt;/ul&gt;
      {% endfor %}
  </code></pre>
  
Tags: <datetime><archives>
Title: Event archives with custom date field
ThreadText: # Event archives with custom date field
  
  (Asked by: username_0 on 2019-01-15)
  
  I'm trying to set up a past events archive. Followed the guide provided by PT: [craft cms guide to set up an archive](https://craftcms.com/guides/creating-an-archive-page-for-entries).
  
  The only difference is that I use a custom date field (dateAndTime) and not postDate. The route leads to the correct template.
  
  **The issue**
  
  When it loads the archives/agenda/2018 template (f. ex.), it displays all 2018 events (grouped by month) but also all 2019 events that where entered in Craft in 2018. This under the correct month, but obviously the wrong year.
  When it load the 2019 template, only the 2019 events that where entered in 2019 show up.
  I can't figure out where the code is wrong.
  
  **Route**
  
  url: archives/agenda/year (token)
  template: \_sections/archives/agenda/index
  
  **Nav**
  
  ```
  {% set firstEntryEver = craft.entries.section('agenda').orderBy('dateAndTime asc').one() %}
      <ul>
      {% for year in now|date('Y') .. firstEntryEver.dateAndTime|date('Y') %}
          <li><a href="archives/agenda/{{ year }}">{{ year }}</a></li>
      {% endfor %}
  </ul>
  
  ```
  
  **Index**
  
  ```
      <h1>{{ year }}</h1>
  
      {% set entriesInYear = craft.entries.section('agenda').limit(null).after(year).before(year+1).all() %}
      {% for month, entries in entriesInYear | group("dateAndTime|date('F')") %}
          <h2>{{ month }}</h2>
          <ul>
              {% for entry in entries %}
                  <li>{{ entry.getLink() }}</li>
              {% endfor %}
          </ul>
      {% endfor %}
  
  ```
  
  **Tags:** datetime, archives
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-25. Score: 2 | Accepted answer
  
  Try this:
  
  ```
  {% set entriesInYear = craft.entries
      .section('agenda')
      .limit(null)
      . dateAndTime([
          'and',
          '>=' ~ year,
          '<' ~ (year + 1)
      ])
      .all()
  %}
  
  ```
  
  The [`.before()`](https://docs.craftcms.com/api/v3/craft-elements-db-entryquery.html#method-before) and [`.after()`](https://docs.craftcms.com/api/v3/craft-elements-db-entryquery.html#method-after) methods are convenience methods that hide the complexity of the date query on Craft's `postDate` field. They don't affect any other custom date fields like your `dateAndTime` field.
  
  The array for your `dateAndTime` field condition (in the above example) is more of a Yii Framework query thing where the optional `'and'` operator in the first position means all of the following conditions must apply. If omitted, it's assumed to be `'or'`.
  
  If you prefer, the array can also be expressed as a string of comma-separated conditions like so:
  
  ```
  .dateAndTime('and, >= ' ~ year ~ ', < ' ~ (year + 1))
  
  ```
  
  
  ---
  
  