Re: Date manipulations in Freemarker Templates

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.devel
Message-ID <[email protected]>
Thursday, October 25, 2012, 10:44:01 PM, Navin wrote:

> Hi,
>   Is there a way to manipulates dates in freemarker template.
>
> I want to add some days/month/year in the current date and want to compare
> it with
> another date. Is there any way to do this?

Sorry, there's no built-in way. (Actually, you could do some tricks
with converting a date to string, increase the day in the string, then
converting it back to a date, because FM supports these conversions...
But it's lame and you had to know how many day a month has.) You will
have write some TemplateMethodModelEx implementations that get a Date
as parameter, do the calendar-math, then return the new Date. Let's
say you write com.example.AddToDateMethodModelEx, then pull it into an
FTL library with
`<#assign addToDate = 'com.example.AddToDateMethodModelEx'?new()>`,
then in the templates you can #import that library like
`<#import '/lib/dateUtils.ftl.' as du>` so you can do
`du.addToDate(someDate, 1)` and such.

> -Navin

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.