Boiler Bill
Boiler Bill

Reputation: 1960

Grails datepicker

Using the grails datepicker control, is there a way to have it save time precision, but only show the user up to the day? The user only cares about that day, but I need to add the time of day that entry was persisted into the db.

Upvotes: 0

Views: 1488

Answers (3)

elixir
elixir

Reputation: 1442

In order to just store precision upto day in the database, you can get use the mapping. Assuming that your database is MSSQL (https://msdn.microsoft.com/en-us/library/ms186724.aspx?f=255&MSPPError=-2147217396)

class Message {

...

Date dateCreated
Date lastUpdated

static mapping = {
    dateCreated sqlType:'date'
    lastUpated sqlType:'date'
}


} 

Ref: http://docs.grails.org/latest/ref/Database%20Mapping/column.html

In your GSPs, you can use

<g:datePicker name="dateCreated" value="${new Date()}" precision="day" />

Ref: http://docs.grails.org/latest/ref/Tags/datePicker.html

Upvotes: 0

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

Reputation: 187379

If you add the attribute precision='day' to the tag, the time fields will not be shown, e.g.

<g:datePicker name="myDate" value="${new Date()}" precision="day"/>

You can set then the time fields in the controller to their default when the data is submitted.

Upvotes: 1

jjczopek
jjczopek

Reputation: 3379

From what I know, datepicker doesnt save precision and sets some default values on the fields that are ommited.

If you need to persist the time of day entry was persisted, you can use dateCreated lastUpdated which will be automatically set when object will be persisted for the first time (dateCreated) or every time object will be modified (lastUpdated).

class Message {

    ...

    Date dateCreated
    Date lastUpdated

} 

Upvotes: 2

Related Questions