Date manipulations in Freemarker Templates

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Date manipulations in Freemarker Templates

Navin
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?

-Navin
Reply | Threaded
Open this post in threaded view
|

Re: Date manipulations in Freemarker Templates

Raymond Auge
You have these built-ins for dates:


As well as the operators <, <=, >, >=, == are supported for dates as stated in this thread:


Sincerely,
- Ray

On Thu, Oct 25, 2012 at 4:44 PM, Navin <[hidden email]> 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?

-Navin



--
View this message in context: http://freemarker.624813.n4.nabble.com/Date-manipulations-in-Freemarker-Templates-tp4654382.html
Sent from the freemarker-devel mailing list archive at Nabble.com.

------------------------------------------------------------------------------
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
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel




------------------------------------------------------------------------------
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
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel
Reply | Threaded
Open this post in threaded view
|

Re: Date manipulations in Freemarker Templates

Daniel Dekany
In reply to this post by Navin
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
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel
Reply | Threaded
Open this post in threaded view
|

Re: Date manipulations in Freemarker Templates

Daniel Dekany
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/
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel
Reply | Threaded
Open this post in threaded view
|

Re: Date manipulations in Freemarker Templates

Navin
Thanks for the reply Daniel and Raymond.

There are some built-ins available for Dates but those can be used only for formatting and
comparisons. We can perform some math calculation after getting month and days from date but it will
add code complexity. I was looking for some built-in that can be used to
add some days/month/year and can give us the future/past date.