Calculation of Dates backwards

I am looking for some help in date calculations.
I have one start date i.e. 01/04/2022. There is one more input field i.e. number of years.

For example, if I select 2 years, then following should be output considering start date is 01/04/2022.

First Year: 01/04/2021 to 31/03/2022
Second Year: 01/04/2020 to 31/03/2021

If I select 3 years, then result should be
First Year: 01/04/2021 to 31/03/2022
Second Year: 01/04/2020 to 31/03/2021
Third Year: 01/04/2019 to 31/03/2020

Please note that I need these dates showing in bold.
I somehow understand that I need to use Repeat function but not sure how to do this.

Any help will be appreciated.

This does what you're looking for :slight_smile:

{formtext: name=year_count; default=3}
{pattern="DD/MM/YYYY";trim=yes}
{start_date="01/04/2022"}
{position_names=["First", "Second", "Third", "Fourth", "Fifth"]}
Dates:
{repeat: for offset in seq(1, year_count); trim=right}
{=position_names[offset]} Year: {time: {=pattern}; shift={=-offset}Y; at={=start_date}; pattern={=pattern}} to {time: {=pattern}; shift={=-offset+1}Y-1D; at={=start_date}; pattern={=pattern}}
{endrepeat}

I am not sure of a way to get "First", "Second", "Third", etc. directly. Seems like that might be an interesting feature addition - to convert a number (1, 2, 3) into its position (first, second, third) :thinking:
For now, I have just hardcoded these names.

1 Like

Thank you Gaurang for the reply and solution.
I was also trying to create formula for this. After using trial and error method, I came up with this.

Start Date: {formdate: DD/MM/YYYY; name=startdate; default=2022-04-01}
End Date:{enddate={time: DD/MM/YYYY; at={=startdate}; pattern=DD/MM/YYYY; shift=+364D}}{=enddate}

Tenure of Previous Appointment: {formtext: name=pyear; default=5}

{repeat: for i in seq(1, {=pyear})}
{=i} Year: {time:DD/MM/YYYY; at={=startdate}; pattern=DD/MM/YYYY; shift=-{=i}Y} to {time: DD/MM/YYYY; at={=enddate}; pattern=DD/MM/YYYY; shift=-{=i}Y}{endrepeat}

Are you seeing any possibility of improving this snippet in any way?

That position "First", "Second" etc is not necessary for the snippet actually. But that will be helpful in future.

1 Like

In addition to this, in Tenure of Previous Appointment, if it is 0 or blank, then it shows this image
I want not to show anything if previous appointment is blank. Any idea?

Yeah, both of our snippets are very nice :grinning_face_with_smiling_eyes: This was a nice application of the repeat command.

1 Like

I guess I will go with your formula. The extra point I added was "enddate" which was not necessary. I simply had to use this one.
{time: {=pattern}; shift={=-offset+1}Y-1D; at={=start_date}; pattern={=pattern}}

1 Like

@Gaurang_Tandon Any idea on this? Will IF work in this?

My bad, missed that message. You will probably need to add a separate if condition outside the repeat:

{formtext: name=pyear}
{if: trim(pyear) <> "" and pyear > 0}
My repeat goes here
{else}
Bad year value
{endif}

1 Like

Thank you

By the way, we can simplify this snippet using a function:

{formtext: name=year_count; default=3}
{pattern="DD/MM/YYYY"; trim=yes}
{start_date="01/04/2022"}
{position_names=["First", "Second", "Third", "Fourth", "Fifth"]}
Dates:
{func=(shift)->{time: {=pattern}; shift={=shift}; at={=start_date}; pattern={=pattern}};trim=right}
{repeat: for offset in seq(1, year_count); trim=right}
{=position_names[offset]} Year: {=func(-offset&"Y")} to {=func((-offset+1)&"Y-1D")}
{endrepeat}

1 Like