+ Start a Discussion
Collen Mayer 6Collen Mayer 6 

Input time on Visual Force Page

Hi All,
I have a object called "time_entry__c" that includes the following fields: date__c, time_in__c, time_out__c.  The date field is date format, and the time in/out fields are date/time format (though if there was a better format option for these fields, they wouldn't have to be date/time).  For a timesheet visual force page I'm creating, for the time_in and time_out I'd like to have the user input the time (in time format, 12:00) and use the date from the date field in what is saved in the date/time format.  Can anyone point me in the right direction, or suggest an easier way that the user can enter time in a visual force page? 

Thanks,
Collen
NagendraNagendra (Salesforce Developers) 
Hi Collen,

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

Apex Controller:
public class MyTimeController {

    public Time myTime { 
        get {
            if (myTime == null) {
                myTime =  DateTime.now().time();
            }
            return myTime;
        } 
        set;
    }
}
Visual Force Page:
<apex:page controller="MyTimeController" docType="html-5.0"> 
   <apex:form>
      Enter Time: <apex:input type="time" value="{!myTime}"/>
    </apex:form>
</apex:page>
Output:
User-added image
Note: 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.

Please let us know if this helps.

Thanks,
Nagendra

 
Collen Mayer 6Collen Mayer 6
Thanks, Nagendra.  So far so good.  So how would I use this on the VF page for existing fields?  So for example, normally, I would use <apex:inputField value="{!TSE.Time_In_1__c}"/> to display the "Time In 1" Field to the user.  How would the tag look to use the time format and allow the user to enter/edit the time on the record? 
Collen Mayer 6Collen Mayer 6
Does any else have thoughts on how I can do this?  Nagendra's code above is working to get the field into a time only format but I am trying to figure out how to use it to edit an existing record from the visual force page.  The sample above for the VF page is:
 
<apex:input type="time" value="{!myTime}"/>

I need to use the input tag with type time instead of the following, which I normally use:
<apex:inputField value="{!TSE.Time_In_1__c}"/>
(TSE.Time_in_1__c is a datetime field). 

Thanks for any code suggestions.

Best,
Collen