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

Hi!

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

When I use the following Twig syntax:

{{ ("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:

{% 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.

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

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

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

same error

Try avoiding set entirely:

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

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

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

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

{% set y = ("now"|date("n") > 1) ? ("now"|date("Y") + 1) : ("now"|date("Y")) %}
@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

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:

@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 :wink:

what you trying to link to

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:

{% 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 :slight_smile:

2 Likes

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:

{{ "/" ~ ({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")) ~ "/" }}
{% 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.

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

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

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

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.

YEAH! i got it

{{ '/' ~ ({'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')) ~ '/' }}
2 Likes