srini
srini

Reputation: 6969

How can i format the date into specified format which comes from db in <g:select>

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

Answers (4)

syllepsa
syllepsa

Reputation: 75

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

Upvotes: 1

D&#243;nal
D&#243;nal

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

Alex
Alex

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

tim_yates
tim_yates

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

Related Questions