I want to offer promotions on my site that always begin and end on the first and last days of the current month.
Getting the first day of the month is trivial (it's always the first), but I'd like to also do the same for the last day of the month.
In the docs it mentions that you can use either Date::Manip or Date::Calc , but based on those documentations I'm not clear on the best way to accomplish this.
What's the best way to show the last day of the month in Template Toolkit?
The way I got this working was basically to load the Date::Calc module through the template toolkit plugin. Then I set "last_day" to the number of days in the month.
The Days_in_Month call requires the date and month (to handle leap years), so I pass the result of date format.
This is the doc containing the format for dates, which is where I got %m.
[% calc = date.calc; last_day = calc.Days_in_Month(date.format(date.now(), "%Y"), date.format(date.now(), "%m")); %]
So to show a range of Month/Day that begins and ends within the current month, I do:
[% date.format(date.now(), "%m/01") %] - [% date.format(date.now(), "%m/") %][% last_day %]
... since the last day of the month will always be double digits, there's no need to pad it with zeros.
Make sure you install the Date::Calc module. If you have it at all, Template Toolkit will load it.