4

According to the docs on apex:inputField: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputField.htm

I can use type="time". However when I try this with a date-time field I get the compliation error:

Expected input type 'datetime-local', got 'time' for Datetime data type

What type of field could I use this with then if not date-time? Am I missing something?

6

There is a Time Class in Apex that you can use. See my example below for how to get the current time displayed in the HTML 5 time input field

Controller:

public class MyTimeController {

    public Time myTime { 
        get {
            if (myTime == null) {
                myTime =  DateTime.now().time();
            }
            return myTime;
        } 
        set;
    }
}  

VisualForce Page:

<apex:page controller="MyTimeController" docType="html-5.0"> 
   <apex:form>
      Enter Time: <apex:input type="time" value="{!myTime}"/>
    </apex:form>
</apex:page>

Output:

Image

NB: HTML5 input tags may not be supported on all browsers. It would be advisable to check a service such as CanIUse to make sure that your target browser supports it.

  • Thanks! Yeah I was probably just going to go with a regular HTML input type of time. However, my question is why is type="time" listed as an option on apex:inputField ? Is there any scenario where that can actually be used? – Phil B Sep 18 '14 at 15:33
  • 1
    One scenario I can think of would be to have a Date input field and a Time input field. The results of which could be combined to produce a DateTime. I never liked SF's field for entering DateTime, its not intuitive to know how to change the time. – BarCotter Sep 18 '14 at 15:37
  • Hm, not quite following, so 2 apex:input fields? How is that using an apex:inputfield? – Phil B Sep 18 '14 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.