0

Is that possible to use only hours? below code I can have hours and minutes but I just need hours to display, is there any attribute that needs to 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>

Apex:

public Time myTime { get; set;}  

2 Answers 2

1

if you don't want to include minutes, you have to skip using type="time".

Instead, use <apex:input type="number"> with html passthrough min and max attributes as shown below. In the controller, you can get the time value by passing the hours which you get from the page.

Apex class:

public class sampleExtension { 

public Integer myTime { get; set;}  

public pagereference clckMe(){
    Time myTime = Time.newInstance(myTime, 0, 0, 0);
    system.debug('==================='+myTime);
    return null; } public sampleExtension (){} }

VF Page:

<apex:form >
      Enter Time: <apex:input type="number" html-min="0" html-max="23" value="{!myTime}"/>
      <apex:commandButton value="Click to get time" action="{!clckMe}"/>
</apex:form>

Hope this helps!

4
  • Compile Error: Illegal assignment from Time to Integer when I try to save the record.. understanable why i'm getting this error... since I already defined Integer
    – Nick
    Jun 6, 2016 at 16:26
  • Could you post your code? Jun 6, 2016 at 17:41
  • i'm trying something like this: myTime = Time.newInstance(myTime, 0, 0, 0) and I'm aware that myTime is Integer and thats why its failing.
    – Nick
    Jun 6, 2016 at 17:44
  • You have to do like: Time myTimeTest = Time.newInstance(myTime, 0, 0, 0); And then use the myTimeTest further. Jun 6, 2016 at 17:55
0

You didn't say whether you were using a custom controller. In apex, you can use myTime.hour() to get the hour component of a time value. I'm not certain if there's an equivalent for use in Visualforce only.

Edit

If you're doing this in your controller as you say, and wanted to display a time, then you should be able to use hours() on your variable if you had one that's holding a value.

The issue you appear to be having is that you're trying to input a time rather than display one. Formatting for display is much easier than assembling one input. I recommend you change your variable to a string.

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

public Time myTime { get; set;} 

Then, inside your controller you can format it as hh:mm by taking myTime and adding +:00 to it so it will look like hh:mm.

1
  • I'm using custom controller insteding of showing HH:MM i just want to show HH only.
    – Nick
    Jun 6, 2016 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .