Reputation: 5554
I need to create a calendar control for selecting Date of Birth. My custom calendar control has fields for setting minDate and maxDate that can be used to set the range of dates that can be selected.
I can get the present date into a variable and use it as maxDate as below.
<#assign dateObject=.now/>
<#assign todaysDate=dateObject?date/>
How can I calculate a date around 100 years prior in a similar way? I read through the official documentation and could not find any operation that could do this.
Upvotes: 0
Views: 1854
Reputation: 31152
You aren't meant to calculate such things in the MVC View... the date range to display should come from the data-model. Anyway, you will need GregorianCalendar
arithmetic for this, and FreeMarker has no such thing built into. But you can write a Java method to do that and then just call that from the template.
BTW, the naive way of doing this is (.now?long - 1000 * 60 * 60 * 24 * daysBefore)?numberToDate
. The problem with this is that you can have a duplicate or skipped day if .now
is near midnight, and some days in the range hit a daylight saving change.
Upvotes: 2