1

I need your help in creating a script on Freemarker with a list of year options based on ${copyrightYear} as a base year. I will use this on credit card expiration options that will have an output like this.

 <option value="" name="">Year</option>
 <option value="2011">2011</option>
 <option value="2012">2012</option>
 <option value="2013">2013</option>
 <option value="2014">2014</option>
 <option value="2015">2015</option>
 ...until 2021

2011 should be ${copyrightYear} and it will increment 10 times until it reaches the year 2021. I want this to be automated every year. Any help from you guys will be much appreciated.Thanks!

user
  • 86,916
  • 18
  • 197
  • 190
liza
  • 101
  • 3
  • 15

1 Answers1

0

Well, assuming your model is setup to use static methods. This should work,

<#setting number_format="##">
<#assign currentYear = statics["java.util.Calendar"].getInstance().getTime()?string("yyyy")?number>
<option value="" name="">Year</option>
<#list currentYear..(currentYear + 10) as year>
    <option value="${year}">${year}</option>
</#list>

There are several ways to get the current year. If you need to, make sure you get the current year in the correct locale.

Edit 1:

As Chaquotay graciously noted, with FreeMarker 2.3.17 .now can be used to get current time instead of the static call to Calendar.

Andy
  • 8,841
  • 8
  • 45
  • 68
  • 1
    With FreeMarker 2.3.17 and newer you don't need ``statics["java.util.Calendar"].getInstance().getTime()`` any more, you can simply use the special variable ``.now``. – Chaquotay Jan 07 '12 at 17:48