Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Date values as params in Grails

I have seen a few posts related to using the g:datePicker in Grails. Using this it looks like you can just pick the value off the params like so params.myDate.

However, when I try to do something like this in my view:

view:

<g:link controller="c" action="a" params="[fromDate:(new Date())]">

controller:

def dateval = params.fromDate as Date

The date is not parsing out correctly. Is there something else I should be doing in the view to make the date 'parsable' by the controller. I've looked around and haven't found this in any posts where datePicker is not used.

like image 452
Scott Avatar asked Dec 03 '22 08:12

Scott


1 Answers

I prefer to send time instead of dates from the client.

<g:link controller="c" action="a" params="[fromDate:(new Date().time)]">

And in action I use the constructor of Date that takes time.

def date = params.date

date = date instanceof Date ? date : new Date(date as Long)

I have created a method in DateUtil class to handle this. This works fine for me.

like image 139
Amit Jain Avatar answered Jan 16 '23 06:01

Amit Jain