Hi,
is there a possibility to count how many friday are there in every current month?
The results should be a specif number (4 or 5) to make an automation.
Any ideas?
Thanks in advance.
Regards,
Alessandro
Hi,
is there a possibility to count how many friday are there in every current month?
The results should be a specif number (4 or 5) to make an automation.
Any ideas?
Thanks in advance.
Regards,
Alessandro
The following should report the number of Fridays in the current month.
{% set nth = 5 %}
{% set month = now().month %}
{% set isoweekday = 5 %}
{% set d = now().date().replace(month=month).replace(day=1) %}
{% set adj = (isoweekday - d.isoweekday()) % 7 %}
{% set fifth = d + timedelta(days=adj) + timedelta(weeks=nth-1) %}
{{ 4 if fifth.month > month else 5 }}
Or the condensed version…
{% set d = now().date().replace(day=1) %}
{% set fifth = d + timedelta(days=((5 - d.isoweekday()) % 7)) + timedelta(weeks=4) %}
{{ 4 if fifth.month > now().month else 5 }}
These are slightly modified versions of Taras’ post found here
EDIT: Addressed redundant replace function brought up by tom_l
I don’t get this bit:
You seem to be replacing the month in this month’s date with this month. Which strikes me as redundant. I’m obviously misunderstanding something.
You’re absolutely right. That was just lazy editing on my part… I was playing around with a “next month” version and just did a find/replace:
{% set next_month = now().month + 1 %}
{%- set d = now().date().replace(month=next_month).replace(day=1) %}
{%- set fifth = d + timedelta(days=((5 - d.isoweekday()) % 7)) + timedelta(weeks=4) %}
{{- 4 if fifth.month > next_month else 5 }}