1

First, I don't have access to the code I have to try to do this on the template.

I have a date that will be available on the template [%UserDate%]

I need to subtract 120 days from the provided [%UserDate%] value.

<div>
120 Days Before:  [%UserDate%]
</div>

I tried to find a way to to this here:
http://template-toolkit.org/docs/modules/Template/Plugin/Date.html
and here:
https://metacpan.org/pod/Date::Calc

But either I am lost or can not find a method or way to do it.

3
  • 1
    What does UserDate actually contain? A string? An epoch time? An object of some sort, and if so what sort? Commented Jun 9, 2023 at 15:41
  • 05/12/2023 a date in that format. Commented Jun 9, 2023 at 18:39
  • 1
    TT offers possibilities to embed perl code: template-toolkit.org/docs/manual/Directives.html#section_PERL Commented Jun 10, 2023 at 11:21

1 Answer 1

2

Assuming a) that UserDate is just a string and nothing useful like a DateTime object and b) that you have Date::Calc installed, then this seems to get you most of the way there:

[% UserDate = '05/12/2023' -%]
[% UserDateArray = UserDate.split('/') -%]
[% USE date -%]
Original date: [% UserDate %]
120 days before: [% date.calc.Add_Delta_Days(UserDateArray.2,
                                             UserDateArray.1,
                                             UserDateArray.0,
                                             -120).reverse.join('/') %]

Testing it with tpage:

$ tpage days.tt
Original date: 05/12/2023
120 days before: 7/8/2023

I've assumed that the date you've mentioned is dd/mm/yyyy. If you're using another, less logical date format then you'll need to adjust the order of the parameters you're passing to Add_Delta_Days().

Sign up to request clarification or add additional context in comments.

4 Comments

Note that you changed the format from having leading zeroes to not having leading zeroes
@ikegami: Yeah. I like to leave something for the OP to do :-)
thanks for the response, After reviewing more UserDate values, I am also getting the dates as Jan, 1. 2023 for example. I am trying to find a way to parse the date like strtotime in php and then format it to the desired way to be able to use it in the Add_Delta_Days method.
@Vidal: Date::Calc also has functions called Decode_Date_EU() and Decode_Date_US() which might be helpful here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.