How can I format the date in the specified format, which comes from db to <g: select>

I need to have a dropdown list that contains dates.

when I write

<g:select id="dob" name="dob" from="${Person.list().dateOfBirth}"  value="${personInstance?.dateOfBirth}" />

displays the date in format 2011-05-17 00:00:00.0But I need to have a format mm/dd/yyyy.

How can i do this?

+3
source share
4 answers

This is the shortest solution that I can come up with (untested):

<g:set var="dateFormat" value="MM/dd/yyyy"/>

<g:select id="dob" name="dob" from="${Person.list().dateOfBirth*.format(dateFormat)}"  
    value="${personInstance?.dateOfBirth?.format(dateFormat)}" />
+3
source

Grails has a formatDate tag that you can use as a method call in this situation. Example:

value="${formatDate(format:'mm/dd/yyyy', date: yourDate)}" />

+1
source
optionValue="${{formatDate(format: 'mm/dd/yyyy', date: it.dateOfBirth)}}"
+1
source

It works?

<g:set var="sdf" value="${new java.text.SimpleDateFormat('MM/dd/yyyy')}"/>
<g:select from="${Person.list()}" name="dob" optionValue="${{sdf.format(it.dateOfBirth)}}" optionKey="${{sdf.format(it.dateOfBirth)}}"/>
0
source

All Articles