Calculating with dates not specific to customers

Hi there,

New to STL: how should one define a fixed date with the code to be able to calculate further with it?

eg date is December, 18th, 2013 (this does not come from input from a customer, but is ‘external’ to him (eg the date where a new tax rule is enforced).

Should this be {% assign Dat_W = ‘2013-12-18’ | Date: ‘%Y/%m/%d’ %} or something else? Thanks!

Hello @Bart_Verhaeghe,

Which calculation are you trying to do here? If you want it to compare to another date, you can just use <= or >= to see if the date is later or earlier than another date.

Also, here you’ll find some more examples to calculate with days and such.

But again, if you explain what calculation exactly you are trying to do here, I could help you more with it :wink:

Hi,

I’m trying to

  • add 15 days to an given date;
  • add 4 days to the same date;

ex 2013-12-18 => 2018-01-02

This date does not come an input tag, but from a certain line in a details table.

We use something like this in our standard text template of “bijzonder verslag” to see if the date of the report is or isn’t within 15 days of the date General Meeting :

{% comment %}datum bijzondere kan nooit korter zijn dan 15 dagen voor datum AV; onderstaande controleert dit{% endcomment %}
{% assign datum_av = period.custom.av.datum | date:'%s' %}
{% assign datum_bijz = company.custom.av.datum_bijzonder | date:'%s' %}

{% assign diffSeconds = datum_bijz | minus: datum_av %}
{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

So the date-tag %s is basically a timestamp in seconds that’s linked to a date (every date gives a separate timestamp in seconds, beginning from somewhere after the seventies I thought).

Anyhow, because of that timestamp, you can calculate to days by dividing by 3600 and 24. If you put that calculation in a variable, you could use something like :

{% if diffDays >= 0 %}
  {% assign na_datum_av = true %}
{% elsif diffDays >= -15 %}
  {% assign 15d_voor_datum_av = true %}
{% endif %}

In our check we’ll use something like :

Dit bijzonder verslag is opgesteld door de {% case director_type %}{% when 'zaakvoerder' %} zaakvoerder{% when 'zaakvoerders' %} zaakvoerders{% else %} raad van bestuur{% endcase %} op {% input company.custom.av.datum_bijzonder as:date %}. {% if 15d_voor_datum_av %}{% ic %}{::warningtext}Opgelet! Datum bijzonder verslag kan niet korter zijn dan 15 dagen voor datum AV{:/warningtext}{% endic %}{% endif %}

Care to try it out @Bart_Verhaeghe?

Let me know if I go too far with this; don’t want to scare you with STL if you’re completely new in it.

Hey Sven,

You’re not going too far with this (on the contrary, I have experience in other programming languages).

Tried it out with success … Thanks … However, now I have other questions:

Suppose you have a start date and you want to calculate and ‘end date’ by first adding 2 days and then 4 years to the start date.

  • First of all, is there a ‘leap year function’ available so you can determine the exact number of days to add for four years?
  • Second, once you have calculated the exact number of days to add to the start date and have added them to the start date, how can you easily determine the date from this number? In Excel, this is done by ‘formatting’ your cell. I wonder whether there’s a similar function in Liquid.

Thanks!

Hi @Bart_Verhaeghe,

We did create an array in Silverfin, called period.calendar_years which contains the following information:

  • start_date
  • end_date
  • amount_of_days
  • amount_of_days_in_full_year

But, that’s only for the current year of course. So that won’t help you, but this might :

{% input custom.some.date as:date %}

{% assign current_date = custom.some.date %}

{% assign date_days = current_date+2 | date:"%d/%m/%Y" %} 
{% assign year = current_date | date:"%Y" %}
  {% assign year_4 = year | plus:4 %}

{% capture date %}{{ date_days | date:"%d/%m" }}/{{ year_4 }}{% endcapture %}

{{ date }}

So you can just add 2 days to your input-date, and then take the year out of that variable to add 4 years to it.

Is this you’re looking for?

I have a feeling you might want to do something else, and take the amount of days of the so-called leap-years into account?

Hey Sven, It’s not exactly what I was looking for, but this solution is close enough. Thanks!

Another related question: the date that has to be manipulated by adding 2 days and 4 years, is not user input, but has to be looked up in the details in another accounts template (more exactly the details in the table ‘Oprichtingen en verhogingen’ the accounts template ‘kapitaal’). How can I do this in code?

Hi @Bart_Verhaeghe,

This is explained in one of our cases here.

Because you already have experience with coding, mind to go at it?

If you’re stuck, let us know and we’ll help you forward. If you have it, you can always post the code here. The more we do that, the more everyone can learn :nerd_face: