0

I am using the Grails plugin, http://grails.org/plugin/jquery-date-time-picker The date and datetime plugin works perfectly well. However, just time has an issue.

I tried to use time only for the datetimepicker. My Config.grovy is as below.

jqueryDateTimePicker {
  format {
    java {
      datetime = "dd/MM/yyyy HH:mm"
      date = "dd/MM/yyyy"
    }
    picker {
      date = "'dd/mm/yy'"
      time = "'hh:mm tt'"
    }
  }
}

My GSP code is something like.

<jqueryPicker:time name="openFrom" id="openFrom" value="${addressInstance?.openFrom}" pickerOptions="[dateFormat: '', timeOnly: true, hourGrid: '4', minuteGrid: '15', timeFormat: 'hh:mm tt']"/>

My Controller is

def addressInstance = new Address(params)

Here in the controller, i can see the params having the time as "7:00 am" but it never gets set in the addressInstance, i believe cause the date is missing.

Abhay
  • 401
  • 3
  • 12

1 Answers1

0

The default Date binding takes the general date format as yyyy-MM-dd hh:mm:ss.S.

In order to to bind openForm date to addressInstance, you can explicitly set it as:

//where params.openForm is String like "7:00 AM"
addressInstance.openForm = Date.parse("h:mm a", params.openFrom)

Date.parse() parses the string as current format to return a Date object. In the aboved case(where you are only concerned about the time), you would end up with the epoch (Jan 1, 1970) with time as 7 AM.

def date = Date.parse("h:mm a", "7:00 AM")
//prints Thu Jan 01 07:00:00 EST 1970

//To get the time string from the date stored in db
date.format("h:mm a") //Prints 7:00 AM

You can also see if you can register a Custom property Editor as shown here to auto bind the date with customized format. You would not like to follow this if you do not want to apply the format to all date strings. In that case, I think the former approach will be useful and easy.

Community
  • 1
  • 1
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Thanks for the quick response. For anyone referring to the solution, there was a typo, below should work. //where params.openForm is String like "7:00 AM" addressInstance.openFrom = Date.parse("h:mm a", params.get('openFrom')) Well i just have time for this instance. Two things here, Date.parse() is kinda deprecated, so any other solution? While displaying the date, post the save, it displays it in the format "01/01/1970 07:00" whereas i would like to display it as "7:00 am" any thoughts on this one? I assume this would be cause i have datetime = "dd/MM/yyyy HH:mm" in my Config.groovy – Abhay Jul 14 '13 at 21:14
  • You might be talking about `parse(String dateStr)`, where as the above answer uses [parse(String format, String dateStr)](http://groovy.codehaus.org/groovy-jdk/java/util/Date.html#parse(java.lang.String, java.lang.String)). Secondly, you can get the time as `7:00 AM` if you use Date's format() method as shown in the answer update. – dmahapatro Jul 15 '13 at 01:59