Templates remaining days and passed days are mixed up

suddenly notice these 2 templates stopped doing their jib correctly:

      remaining_days:
        friendly_name: Remaining days
        value_template: >
          {% set this = now() %}
          {% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
          {% set last = this.replace(month=next, day=1) %}
          {{(last.date() - this.date()).days}}

      past_days:
        friendly_name: Past days
        value_template: >
          {% set this = now() %}
          {% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
          {% set first = this.replace(day=1) %}
          {{(this.date() - first.date()).days}}

what could have changed for this too happen??

It’s not set up for next year.

          {% set this = now() %}
          {% set next_month = this.month + 1 if this.month + 1 <= 12 else 1 %}
          {% set next_year = this.year + 1 if this.month == 12 else this.year %}
          {% set last = this.replace(year=next_year, month=next_month, day=1) %}
          {{(last.date() - this.date()).days}}

really sorry, but I am confused…

this template used to show the remaining days of the current month did it, just as the other template shows the passed days of the current month, not passed and remaining days of the year?

found your original post on it
think I took out all the replace() myself in a later stage.

btw it says 15, now, while it should be 14?

Yes but what’s the difference between January 1st 2020 and December 17th 2020? Answer -351 days.

When you cross the year, you need to add 1 to the year… January 1st 2021 - December 17th 2020 = 15 days

I count 15…

thanks, I guess that depends on whether you include the (current, already started) day, or start counting the next (remaining) day.

31 - 17 = 14. But remember, you’re counting to January 1st not December 31st… which is essentially 32-17.

If you want to not include the 1st of the month in the calc, just subtact 1 from the result line:

{{ (last.date() - this.date()).days - 1 }}

yeah, Understood.
nothing new to that confusion: Templating Help reading the previously linked topic a few posts further down :wink:

well, everyone thinks the calc is including the ‘current day’ when in reality it’s including the first of the next month.

yes, you’re right again :wink:
good to stress these things, as they are fundamental to the calculations.

as for the passed days template, why did you add the next phrase there at all? Seems

          {% set this = now() %}
          {% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
          {% set first = this.replace(day=1) %}
          {{(this.date() - first.date()).days}}

can safely be replaced by:

          {% set this = now() %}
          {% set first = this.replace(day=1) %}
          {{(this.date() - first.date()).days}}