Reputation: 6969
I need to have a dropdown which holds dates.
when i write
<g:select id="dob" name="dob" from="${Person.list().dateOfBirth}" value="${personInstance?.dateOfBirth}" />
this displays the date in the format 2011-05-17 00:00:00.0
But i need to have the format mm/dd/yyyy
.
How can i do that?
Upvotes: 3
Views: 4281
Reputation: 75
optionValue="${{formatDate(format: 'mm/dd/yyyy', date: it.dateOfBirth)}}"
Upvotes: 1
Reputation: 187399
This is the shortest solution I can think of (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)}" />
Upvotes: 3
Reputation: 53
Grails has a formatDate Tag which you can use as a method call in this situation. Example:
value="${formatDate(format:'mm/dd/yyyy', date: yourDate)}" />
Upvotes: 1
Reputation: 171154
Does this work?
<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)}}"/>
Upvotes: 0