# Need help with Twig year logic for date calculation in Text Component

**URL:** <https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054>\
**Category:** Elements\
**Created:** [February 2, 2026, 5:01pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054 "2026-02-02T17:01:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 2, 2026, 5:01pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/1 "2026-02-02T17:01:32Z")

</div>

Hi!

I’d like to implement a small **year-based logic** using Twig syntax.

When I use the following Twig syntax:

```auto
{{ ("first sunday of january " ~ y)|date("d.m.Y") }}

```

I get the date of the **first Sunday in January**. Since January has already passed this year, I’d like it to automatically return the date of the **next first Sunday in January of the following year**.

I found the following approach, which should handle this logic:

```auto
{% set y = ("now"|date("n") > 1) ? ("now"|date("Y") + 1) : ("now"|date("Y")) %}
{{ ("first sunday of january " ~ y)|date("d.m.Y") }}

```

However, when I insert this syntax into a **Text Component** , it doesn’t work and I only get an error message.

 ![Bildschirmfoto 2026-02-02 um 17.53.41](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/9/c/9c3f7163d21310355a5ccb2c3abc5f5bb3a46ed5.jpeg)

Is there a Twig expert here who could give me a few hints on what I might be doing wrong or how to best implement this?

Alternatively, the question is also directed at @Dan or @Ben, if you happen to have the time.

Thanks

---

<div class="post-metadata">

**Author:** ![PMUK](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@PMUK](https://forums.realmacsoftware.com/u/PMUK)\
**Post date:** [February 2, 2026, 6:04pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/2 "2026-02-02T18:04:42Z")

</div>

{% set y = (“now”|date(“n”) \> 1)  
? (“now”|date(“Y”) + 1)  
: (“now”|date(“Y”))  
%}

{{ ("first sunday of january " ~ y)|date(“d.m.Y”) }}  
Try this

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 2, 2026, 7:01pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/3 "2026-02-02T19:01:03Z")

</div>

same error

---

<div class="post-metadata">

**Author:** ![Doobox](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/doobox/32/32373_2.png) [@Doobox](https://forums.realmacsoftware.com/u/Doobox)\
**Post date:** [February 2, 2026, 8:09pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/4 "2026-02-02T20:09:19Z")

</div>

Try avoiding set entirely:

```auto
{{ (
  "first sunday of january " ~
  (("now"|date("Y")|int) + (("now"|date("n")|int > 1) ? 1 : 0))
)|date("d.m.Y") }}

```

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 2, 2026, 8:23pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/5 "2026-02-02T20:23:50Z")

</div>

Thanks for your feedback — I really appreciate it.

Unfortunately, none of this works. I suspect that this Twig syntax is not available or enabled in Elements, which is why it doesn’t work

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 2, 2026, 8:27pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/6 "2026-02-02T20:27:01Z")

</div>

```auto
{{ ("first sunday of january " ~ y)|date("d.m.Y") }}

```

This Twig syntax works on its own, but not in combination with:

```auto
{% set y = ("now"|date("n") > 1) ? ("now"|date("Y") + 1) : ("now"|date("Y")) %}

```

---

<div class="post-metadata">

**Author:** ![upssjw](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/upssjw/32/26063_2.png) [@upssjw](https://forums.realmacsoftware.com/u/upssjw)\
**Post date:** [February 2, 2026, 8:40pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/9 "2026-02-02T20:40:40Z")

</div>

```auto
@raw()
{% if "now"|date("m") == "01" %}
    {% set targetYear = "now"|date("Y") %}
{% else %}
    {% set targetYear = "now"|date("Y") + 1 %}
{% endif %}

{{ date("first sunday of January " ~ targetYear)|date("d.m.Y") }}
@endraw

```

the previous version was the 4th Monday try this in html component, could add some controls

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 2, 2026, 9:29pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/10 "2026-02-02T21:29:48Z")

</div>

Thanks for that, Steve. Using the **HTML component** , the date output itself works without any issues.

Unfortunately, with the HTML component I can’t really move forward with my website layout in the way I’d like.

At the latest when I try to link the date like this:

```auto
@raw()
<a href="/events/?date={% if "now"|date("n") == "1" %}{{ "first sunday of january now"|date("Y-m-d") }}{% else %}{{ "first sunday of january next year"|date("Y-m-d") }}{% endif %}">
  {% if "now"|date("n") == "1" %}
      {{ "first sunday of january now"|date("d.m.Y") }}
  {% else %}
      {{ "first sunday of january next year"|date("d.m.Y") }}
  {% endif %}
</a>
@endraw

```

I end up in a dead end again — even though I was actually a bit further along with the **Text component**.

Since the HTML component and I aren’t really friends, I’ll probably go with a different, simpler approach for now and just set the date manually in my Markdown file.

Still, thanks a lot — and I’m always impressed by your talent 😉

---

<div class="post-metadata">

**Author:** ![upssjw](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/upssjw/32/26063_2.png) [@upssjw](https://forums.realmacsoftware.com/u/upssjw)\
**Post date:** [February 2, 2026, 9:56pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/11 "2026-02-02T21:56:47Z")

</div>

what you trying to link to

---

<div class="post-metadata">

**Author:** ![ben](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/ben/32/28488_2.png) [@ben](https://forums.realmacsoftware.com/u/ben)\
**Post date:** [February 3, 2026, 9:20am UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/12 "2026-02-03T09:20:48Z")

</div>

> [@Pegasus](#):
>
> ```php-template
> {% set y = ("now"|date("n") > 1) ? ("now"|date("Y") + 1) : ("now"|date("Y")) %}
> {{ ("first sunday of january " ~ y)|date("d.m.Y") }}
> 
> ```

The issue is that Elements is encoding the `>` in to `&gt;` which is why you’re getting the “Unexpected character ‘&’” error.

So this isn’t an issue with your Twig code. I don’t think this is a “bug” either, as it’s expected behaviour for Elements to encode HTML characters in this way.

What might work for you is to modify the check to be `!=` instead of `>`, so:

```php
{% set y = ("now"|date("n") != 1) ? ("now"|date("Y") + 1) : ("now"|date("Y")) %}
{{ ("first sunday of january " ~ y)|date("d.m.Y") }}

```

that works for me locally, give it a try and let me know 🙂

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 3, 2026, 11:21am UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/13 "2026-02-03T11:21:54Z")

</div>

Great! Thanks, Ben. That was exactly the right hint — it works both locally and online now.

However, my final goal hasn’t been fully achieved yet. The ultimate goal is to generate a corresponding **URL slug** from this date.

For example:

- **22 February 2026** → /februar/22-02-2026/
- **01 March 2026** → /maerz/01-03-2026/

To achieve this, I came up with the following Twig syntax:

```auto
{{ "/" ~ ({1:"januar",2:"februar",3:"maerz",4:"april",5:"mai",6:"juni",7:"juli",8:"august",9:"september",10:"oktober",11:"november",12:"dezember"}[(("this sunday +2 weeks")|date("n"))]) ~ "/" ~ (("this sunday +2 weeks")|date("d-m-Y")) ~ "/" }}

```

```auto
{% set dt=("this sunday +2 weeks")|date %}
{% set slugs={1:"januar",2:"februar",3:"maerz",4:"april",5:"mai",6:"juni",7:"juli",8:"august",9:"september",10:"oktober",11:"november",12:"dezember"} %}
{{ slugs[dt|date("n")] ~ "/" ~ dt|date("d-m-Y") }}

```

Both versions work perfectly when output in a **Text Component** or a **Button Component** , producing the expected result (e.g. /maerz/01-03-2026/).

However, when I try to use the same Twig syntax inside a **Link / Plain Text** field, I get an error message.

 ![Bildschirmfoto 2026-02-03 um 11.56.17](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/5/2/529ee5f85764ec7925bc2692762baf029d537f7e.jpeg)

 ![Bildschirmfoto 2026-02-03 um 12.20.31](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/7/d/7d11105a32a35fb1157a2ca1dc0d6d285eaf402d.jpeg)

What _does_ work in the Link / Plain Text field is the following:

```auto
{{ "/" ~ (("this sunday +2 weeks")|date("d-m-Y")) ~ "/" }}

```

which correctly outputs /22-02-2026/, but obviously without the month slug.

 ![Bildschirmfoto 2026-02-03 um 12.23.03](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/c/d/cd42cf1d495f2b45c13ed3b8f6a1ec709d69dd1d.jpeg)

 ![Bildschirmfoto 2026-02-03 um 12.25.09](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/e/f/ef6f4a2878f44a70cf69fbf5499546dab7169046.jpeg)

I would really appreciate further help here — especially any tips on how this could be solved cleanly using Twig.

At the moment, I’m using a JavaScript workaround, but I’d very much like to replace that with a proper Twig-based solution in the future.

Thanks again in advance for any help or ideas.

---

<div class="post-metadata">

**Author:** ![Pegasus](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/pegasus/32/25035_2.png) [@Pegasus](https://forums.realmacsoftware.com/u/Pegasus)\
**Post date:** [February 3, 2026, 1:02pm UTC](https://forums.realmacsoftware.com/t/need-help-with-twig-year-logic-for-date-calculation-in-text-component/54054/14 "2026-02-03T13:02:17Z")

</div>

YEAH! i got it

```auto
{{ '/' ~ ({'1':'januar','2':'februar','3':'maerz','4':'april','5':'mai','6':'juni','7':'juli','8':'august','9':'september','10':'oktober','11':'november','12':'dezember'}[(('this sunday +2 weeks')|date|format_datetime(pattern:'M'))]) ~ '/' ~ (('this sunday +2 weeks')|date|format_datetime(pattern:'dd-MM-yyyy')) ~ '/' }}

```
