Week numbers doesn't match

The week number in HA does not match the week number in Outlook Agenda.

Today, April 28, is week 17 according to HA, while Outlook Agenda says it is week 18.
{{ now().strftime('%W') }}

Can this be fixed or is it a bug?

According to this, it is week 18

https://www.epochconverter.com/weeks/2025

ISO 8601 is not the only week numbering system in the world, other systems use weeks starting on Sunday (US) or Saturday (Islamic).

So it could be a bug, or just using another system.

The ISO week numbering standard are based on these rules:

An ISO week will always have seven days that can contain days from the current month as well as the next month. This is true of days that overlap from the last and first months of the year.
A week cannot be broken; there are no partial weeks that contain only four or five days.
The ISO week always starts on Monday and ends on Sunday.

Following the American standard, these rules apply:

A week starts on Sunday and can have less than seven days in that week. This is called a partial week.
A week can begin on a day other than a Sunday, although Sunday is considered the first day of the calendar week. However, a week that starts in the middle will behave as though part of the same week number as it’s predecessor week.
Another point about the US partial week standard is that the week containing January 1st is always reflected as the first week of the next year. This case is true even if the first day begins in the middle of the week.

Thanks for the reply.
I live in the Netherlands and suppose I live in the US than today should be week number 18 also… It’s Monday, not Sunday. Right?

Right. So probably a bug .

The start of the week is not the only thing that determine the week number.
It can also be the first 3 or 4 day week that counts as number 1.

Alternatively it can also be that the output counts from 0 and not 1, dobbelt hich is th case here.
https://www.man7.org/linux/man-pages/man1/date.1.html

Nice, but how to fix it?
You refer to Linux manual, but I use Home Assistant OS installation, not Home Assistant OS Container.

You need to use the ISO week number.

{{ now().isocalendar()[1] }}

or

{{ now().isocalendar()["week"] }}
1 Like

Thank you.
‘Issue’ solved.
(Can’t find the “Answer check box” :cry:)

Probably because the thread is not categorized.

+1 to week would have solved it too.
HA is running on Linux and Python and use the same libraries.

Yes this year.
But some other year you would have to remove the +1

The Linux numbering will always start with 0.
It is the definition of what is considered the first week that makes the problem and Linux use the first Monday as a starting point.
isocalendar use the first week that contains a thursday as a starting point and there are other variants.

The original code {{ now().strftime('%W') }} could be fixed with my link as the other format options are listed there, like %V for ISO week.

Yes and that is why you should use isoweek if you live in a country that uses isoweek.
This is obviously not just an issue for Linux, it’s how calendar works.

Discussions about agreeing the most are always so confusing. :smiley:

After reading this thread further, I now understand the link you posted.
So using %V is better than %W, because ISO week numbers start with 01, if I understand correctly?

Yup, it gives you the result according to a ISOCalendar, just like the one Hellis81 referred to.
Both ways work.