datefreemarkerweek-number

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


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?


Solution

  • 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.