Re: Date manipulations in Freemarker Templates

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.devel
Message-ID <[email protected]>
Friday, October 26, 2012, 2:18:19 PM, Daniel Dekany wrote:

> 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.

It just comes to my mind... For a while FreeMarker can convert dates
to longs, and longs back to dates. So you can do some math when it's
in long form.

The relevant built-ins are someDate?long and someLong?number_to_date,
?number_to_datetime, ?number_to_time.

> (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


------------------------------------------------------------------------------
The Windows 8 Center 
In partnership with Sourceforge
Your idea - your app - 30 days. Get started!
http://windows8center.sourceforge.net/
what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
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.