<p>To debug issues like this, you can put <a href="https://craftcms.com/docs/3.x/dev/tags.html#dd" rel="nofollow noreferrer"><code>{% dd %}</code> tags</a> in your template to inspect the value of variables and find errors in your logic:</p>
<pre><code>{% dd currentDate %}
</code></pre>
<p>The main issue with your template is that you're comparing dates <em>as formatted strings</em>. The <a href="https://twig.symfony.com/doc/2.x/filters/date.html" rel="nofollow noreferrer"><code>date()</code> filter</a> returns a formatted, human-readable string (so something like <em>1 January 2021</em>, the format depends on your locale). Those can't be directly compared, so the <code>&lt;</code> / <code>&gt;</code> checks fail.</p>
<p>Instead, you want to compare timestamps as <code>DateTime</code> objects, which is also the format used by Craft's date fields and properties by default. So removing the <code>|date</code> filter from your variables should fix the issue:</p>
<pre><code>{% set start = entry.eventStartDate %}
{% set currentDate = now %}
{% set futureDate = currentDate|date_modify(&quot;+30 day&quot;) %}
</code></pre>
<p>Note the use of <code>now</code> instead of <code>'now'</code>. The <code>now</code> variable is <a href="https://craftcms.com/docs/3.x/dev/global-variables.html#now" rel="nofollow noreferrer">provided by Craft</a> and contains the current timestamp as a <code>DateTime</code> object.</p>
<hr />
<p>However, instead of fetching all entries and filtering them out afterwards, it would be more efficient to include the acceptable date range as a filter in your query. Something like this should work:</p>
<pre><code>{% set futureDate = currentDate|date_modify(&quot;+30 day&quot;) %}
{% set myEntryQuery = craft.entries()
    .section('events')
    .hidden('not true')
    .orderBy('eventStartDate')
    .eventStartDate(['and', &quot;&gt;= #{now|atom}&quot;, &quot;&lt;= #{futureDate|atom}&quot;])
%}
</code></pre>
<p>See <a href="https://craftcms.com/docs/3.x/date-time-fields.html#querying-elements-with-date-fields" rel="nofollow noreferrer">Querying Elements with Date Fields</a> for more information.</p>

CC BY-SA 4.0
2021-09-02T16:03:01.810Z
2021-09-02T16:03:01.810Z
MoritzLost
<p>I'm creating a date range where I only want to show upcoming events in the next 30 days based on a start date.</p>
<p>I have created my query but when I add my if statement to filter on the start date it doesn't show the upcoming events. I'm not sure what I am doing wrong.</p>
<pre><code>{% set myEntryQuery = craft.entries()
    .section('events')
    .hidden('not true')
    .orderBy('eventStartDate')
%}

{% set entries = myEntryQuery.all() %}

{% for entry in entries %}
{% set start = entry.eventStartDate | date() %}
{% set currentDate = &quot;now&quot;|date() %}
{% set futureDate = currentDate|date_modify(&quot;+30 day&quot;)|date() %}


{% if (start &gt;= currentDate) and (start &lt; futureDate) %}
    &lt;item&gt;
        &lt;title&gt;{{ title }}&lt;/title&gt;
        &lt;link&gt;{{ url }}&lt;/link&gt;
        &lt;pubDate&gt;{{ entry.postDate|rss }}&lt;/pubDate&gt;
        &lt;author&gt;CPI&lt;/author&gt;
        &lt;guid&gt;{{ url }}&lt;/guid&gt;
        &lt;description&gt;
        ---------
            {% if eventType == 'Events' %}
            {% else %}
            {{ eventType }}
            {% endif %}
            {% if start %}
             · {{ start }}
            {% endif %}
            {% if location %}
             · {{ location }}
            {% endif %}
        ---------
        &lt;/description&gt;
    &lt;/item&gt;
{% endif %}

{% endfor %}
</code></pre>

<events><queries>
Craft Event Date Range in Craft
# Craft Event Date Range in Craft

(Asked by: username_0 on 2021-09-02)

I'm creating a date range where I only want to show upcoming events in the next 30 days based on a start date.
I have created my query but when I add my if statement to filter on the start date it doesn't show the upcoming events. I'm not sure what I am doing wrong.
{% set myEntryQuery = craft.entries()
    .section('events')
    .hidden('not true')
    .orderBy('eventStartDate')
%}

{% set entries = myEntryQuery.all() %}

{% for entry in entries %}
{% set start = entry.eventStartDate | date() %}
{% set currentDate = "now"|date() %}
{% set futureDate = currentDate|date_modify("+30 day")|date() %}


{% if (start >= currentDate) and (start < futureDate) %}
    
        {{ title }}
        {{ url }}
        {{ entry.postDate|rss }}
        CPI
        {{ url }}
        
        ---------
            {% if eventType == 'Events' %}
            {% else %}
            {{ eventType }}
            {% endif %}
            {% if start %}
             · {{ start }}
            {% endif %}
            {% if location %}
             · {{ location }}
            {% endif %}
        ---------
        
    
{% endif %}

{% endfor %}

**Tags:** events, queries


---

## 1 answers

### Answer by username_1 on 2021-09-02. Score: 1

To debug issues like this, you can put {% dd %} tags in your template to inspect the value of variables and find errors in your logic:
{% dd currentDate %}

The main issue with your template is that you're comparing dates as formatted strings. The date() filter returns a formatted, human-readable string (so something like 1 January 2021, the format depends on your locale). Those can't be directly compared, so the < / > checks fail.
Instead, you want to compare timestamps as DateTime objects, which is also the format used by Craft's date fields and properties by default. So removing the |date filter from your variables should fix the issue:
{% set start = entry.eventStartDate %}
{% set currentDate = now %}
{% set futureDate = currentDate|date_modify("+30 day") %}

Note the use of now instead of 'now'. The now variable is provided by Craft and contains the current timestamp as a DateTime object.

However, instead of fetching all entries and filtering them out afterwards, it would be more efficient to include the acceptable date range as a filter in your query. Something like this should work:
{% set futureDate = currentDate|date_modify("+30 day") %}
{% set myEntryQuery = craft.entries()
    .section('events')
    .hidden('not true')
    .orderBy('eventStartDate')
    .eventStartDate(['and', ">= #{now|atom}", "<= #{futureDate|atom}"])
%}

See Querying Elements with Date Fields for more information.


---

