Leap year : function available

Is there a default function to check if a calendar year is a leap year?
Now in a lot of templates the calculation uses 365 but for leap years it should be 366 ?
Before doing the calculation myself …

Hello @Katrien,

There are several ways to accomplish this; I’ll list a few :

{% assign total_days_in_book_year = period.end_date-period.year_start_date+1 %}
{% assign calendar_year_start_date = period.year_end_date | date:"1/1/%Y"%}
{% assign calendar_year_end_date = period.year_end_date | date:"31/12/%Y"%}
{% assign total_days_in_year = calendar_year_end_date-calendar_year_start_date+1 %}

This calculates the amount of days (you can see this in our template of “NIA”).

But you could also use this, in case it’s always either 365 or 366 :

{% assign by = period.year_end_date | date:'%Y' %}

{% if by == '2016' %}
  {% assign days = 366 %}
{% else %}
  {% assign days = 365 %}
{% endif %}

{{ days }}

It all depends how you need to use it. Does this help you further?

Let us know is something is unclear.

Okay that was also my emergency solution as mentioned above. But I have to make a calculation because I do not know the year, it can be 2000 or 2020 or wathever
, so I have to calculate if a year is a leap year using a formula. Vb IsLeapYear (2020). Because i did not want to reinvent the warm water … this question.

Then I guess the first option is the right for you @Katrien

If you need anything else related to this, just let us known.

This can be done with only one line of code

{% assign days_in_year = yourinputdate | date:"31/12/%Y" | date:"%j" | convert: "number" %}

This will give you the days of a year for a particular date, so 365 or 366 for a leap year.

1 Like

@Peter

thank you for your insights on this topic. We can attain the amount of days in a year (or bookyear) in different ways.

One way I prefer is looping over the collection calendar_years. As the name says, this gives you the details over your current calendar year.

{% for calendar in period.calendar_years %}
Start date : {{ calendar.start_date }}
End date : {{ calendar.end_date }}
Amount of days in bookyear : {{ calendar.amount_of_days }}
Amount of days in full year : {{ calendar.amount_of_days_in_full_year }}
{% endfor %}

Which gives you the following result :

Start date : 2019-01-01
End date : 2019-31-12
Amount of days in bookyear : 365
Amount of days in full year : 365

Kind regards
Sofie

Yes, I know but that is more code to write and also you can not check for a date outside the current bookyear. With this code you can check every date if it’s in a leap year.

1 Like