Get the ISO 8601 Week of the Year in FreeMarker based on a date

126 Views Asked by At

I need to get the current week of the year based on a string date. E.g. 07.03.2023Week 10 of 2023.

In the docs I found many helpers, but none for the week of the year.

<#assign aDate = theDate?date>
<!-- All kind of day of the week, month, year etc, -->
${aDate?string["EEE, MMM d, ''yy"]}
<!-- How to get the week of the year? -->

How could I get the week of the year of a date using just the FreeMarker template?

1

There are 1 best solutions below

1
Jasper de Vries On BEST ANSWER

FreeMarker uses the Java simple date format patterns to format dates. You can use the w pattern to get the "week in year". So you can simply do:

Current week number: ${.now?string['w']}

As Basil pointed out, you want to make sure you are using the right locale. There are several ways to set the locale in FreeMarker.