How to make a default select tag for the current year and current month?

I have 2 select tags as,

 <td><g:select name="Year" from="${2004..2014}"  optionValue="${Year}" /></td>
  <td><g:select name="Month" from="${['January','February','March','April','May','Jun','July','August','September','October','November','December']}"  optionValue="${month}"  /></td>

Now I want gsp to show me the current year and month in the year and month selection field instead of displaying the first year and month in the list.

how to reach it? .. can someone answer me please.

thanks in advance Lakshmi

+3
source share
2 answers

try it

<g:set var="months" value="${new java.text.DateFormatSymbols().months}"/>
<g:set var="today" value="${new Date()}"/>

<td><g:select name="Year" from="${2004..2014}" value="${today[Calendar.YEAR]}" /></td>

<td><g:select name="Month" from="${months as List}" 
    value="${months[today[Calendar.MONTH]]}"  /></td>
+12
source

In the controller, create a date:

def year = new Date().format("yyyy")
def month = new Date().format("MM")
[year:year, month:month]

Should provide you with the current month and year.

+1
source

All Articles