<p>This conditional should work:</p>

<pre><code>{# Saison Trigger #}
{% set saison = now|date('m-d') %}

{# Check for each Season #}
{% if saison &gt; '11-22' or saison &lt;= '03-21' %}
    {% set currentSaison = 'winter' %}
{% elseif saison &gt; '03-21' and saison &lt;= '06-21' %}
    {% set currentSaison = 'fruehling' %}
{% elseif saison &gt; '06-21' and saison &lt;= '09-22' %}
    {% set currentSaison = 'sommer' %}
{% elseif saison &gt; '09-22' and saison &lt;= '11-22' %}
    {% set currentSaison = 'herbst' %}
{% endif %}
</code></pre>

<p>You're right, Twig does just compare the strings. But this is totally fine in this case. A string representing a date and formatted like so "01-13" (don't forget the leading zero) works nicely with the comparison operators.</p>

CC BY-SA 3.0
2016-01-12T11:18:11.533Z
2016-05-24T12:26:38.957Z
2016-05-24T12:26:38.957Z
carlcs
<p>I want to check if it's winter. What is the best way to write a "between dates" conditional?</p>

<pre><code>{% set saison = now|date('m-d') %}
{% if saison (saison &gt;= '11-22' and saison &lt;= '03-21') %}
  Jep it's winter
{% endif %}
</code></pre>

<p>This absolutely does not work.
I also tried somthing like:</p>

<pre><code>{% set saison = now|date('m-d') %}
{% if saison &gt; '03-21' %}
  Now it's Spring
{% elseif saison &gt; '06-21' %}
  Now it's Summer
{% elseif saison &gt; '09-23' %}
  Now it's Fall
{% elseif saison &gt; '12-22' %}
  Now it's Winter
{% endif %}
</code></pre>

<p>That doesn't work. I guess because twig doesn't recognize the strings as dates?
How do I have to write this?</p>

<datetime><conditional>
If date is between conditional
# If date is between conditional

(Asked by: username_0 on 2016-01-12)

I want to check if it's winter. What is the best way to write a "between dates" conditional?
{% set saison = now|date('m-d') %}
{% if saison (saison >= '11-22' and saison <= '03-21') %}
  Jep it's winter
{% endif %}

This absolutely does not work.
I also tried somthing like:
{% set saison = now|date('m-d') %}
{% if saison > '03-21' %}
  Now it's Spring
{% elseif saison > '06-21' %}
  Now it's Summer
{% elseif saison > '09-23' %}
  Now it's Fall
{% elseif saison > '12-22' %}
  Now it's Winter
{% endif %}

That doesn't work. I guess because twig doesn't recognize the strings as dates?
How do I have to write this?

**Tags:** datetime, conditional


---

## 1 answers

### Answer by username_1 on 2016-01-12. Score: 4 | Accepted answer

This conditional should work:
{# Saison Trigger #}
{% set saison = now|date('m-d') %}

{# Check for each Season #}
{% if saison > '11-22' or saison <= '03-21' %}
    {% set currentSaison = 'winter' %}
{% elseif saison > '03-21' and saison <= '06-21' %}
    {% set currentSaison = 'fruehling' %}
{% elseif saison > '06-21' and saison <= '09-22' %}
    {% set currentSaison = 'sommer' %}
{% elseif saison > '09-22' and saison <= '11-22' %}
    {% set currentSaison = 'herbst' %}
{% endif %}

You're right, Twig does just compare the strings. But this is totally fine in this case. A string representing a date and formatted like so "01-13" (don't forget the leading zero) works nicely with the comparison operators.


---

