<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>

CC BY-SA 4.0
2019-01-25T15:27:21.843Z
2019-01-25T15:32:31.910Z
2019-01-25T15:32:31.910Z
Sam Hernandez
<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>

<datetime><archives>
Event archives with custom date field
# 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.
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() %}
    
    {% for year in now|date('Y') .. firstEntryEver.dateAndTime|date('Y') %}
        {{ year }}
    {% endfor %}


Index
    {{ year }}

    {% set entriesInYear = craft.entries.section('agenda').limit(null).after(year).before(year+1).all() %}
    {% for month, entries in entriesInYear | group("dateAndTime|date('F')") %}
        {{ month }}
        
            {% for entry in entries %}
                {{ entry.getLink() }}
            {% endfor %}
        
    {% 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() and .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))


---

