2

I am trying to format the new Time field type in Visualforce but with no success.

I'm trying to use the same method as i use for date formatting but it does not compile my code with the error:

'The value attribute on <apex:outputText> is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.'.

And i can't seem to find any documentation about this new field type or formatting only time variables. My code atm:

<apex:outputText value="{0,time,HH':'mm}">
     <apex:param value="{!obj.Time_field__c}"/>
</apex:outputText>
2
  • whats the exact issue you are facing. You are question isnt so clear.<apex:outputText value="{0,time,HH:mm }"> <apex:param value="YOUR DATE TIME FIELD" /> </apex:outputText>...
    – Annappa
    Dec 12, 2017 at 11:28
  • the issue is that i wish to apply formating to my time field and im not able to. is this a limitation of the time field type or am i doing something wrong?
    – user37586
    Dec 12, 2017 at 11:54

3 Answers 3

1

I think this is a bug or an oversight, but I can't really prove it.

The Time class in Salesforce is very limited, especially compared to the DateTime class. It doesn't even have its own format method, which I think may be the underlying problem here. Without a format method to accept the formatting string, Salesforce might not be able to properly format the field, but the error reported doesn't seem to relate.. although a value which cannot be formatted could be considered an invalid value in this case.

I also think the Time option for the apex:outputText was not meant for this field, but rather, the Time component of a DateTime class. For example, in the code below, I'm using the Time option in the value, but I'm using a DateTime field, and it renders without any problems.

<apex:outputText value="{0, Time, HH:mm:ss z}">
    <apex:param value="{!someAccount.CreatedDate}"/> <!-- Date/Time field --> 
</apex:outputText>

The same outputText causes a compile error if we try to use a Time field instead.

<apex:outputText value="{0, Time, HH:mm:ss}">
    <apex:param value="{!someAccount.Test_Time__c}"/> 
</apex:outputText>

The value attribute on <apex:outputText> is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.


As for a solution, I would do the formatting myself in the controller, instead of relying on the visualforce to do it for me.

Apex:

public String getFormattedTime() {
    return SomeAccount.Test_Time__c.hour() + ':' + SomeAccount.Test_Time__c.minute(); 
}

Visualforce:

<apex:outputText value="{!FormattedTime}" />
4
  • 1
    TBH this was frustrating me so much, I created a wrapper class that had everything I needed. It has a getter that returns a DateTime which is compatible with VisualForce formatting so that you can extract time-only fields and work with it as needed. For inputs it was even more painful though. I ended up with a multitude of extra getters and setters just to make it possible to pick a time in the UI and bind it to a record.
    – Charles T
    Dec 12, 2017 at 15:28
  • If there's an existing idea, add a comment with a link to it and I'll edit it into my answer, otherwise it might be a good idea to make an idea or a bug report and see if it can get some traction. Dec 12, 2017 at 15:35
  • I'd also add that the Apex Time class isn't sortable! (or at least wasn't in the previous API version, but I doubt that's changed) I had to create a wrapper just to sort a list of times.
    – Charles T
    Dec 12, 2017 at 16:06
  • 1
    Thanks for the explanation! It makes sense that visualforce can not format a time variable then, if it doesn't even have its own format method. I can't do it on the server side since, i need to use it inside an apex:repeat component, but i figured out if you use an outputField instead it renders the time in a somewhat standard format. Which will have to do for now. Hope they develop a little bit more of this class since now its a standard field type :) Thanks!
    – user37586
    Dec 13, 2017 at 10:01
3

Pure VisualForce solution, if you need to do this without any Apex:

{!HOUR(Object__c.Time_Field__c)}:
<apex:outputText value="{0,number,00}">
    <apex:param value="{!Minute(Object__c.Time_Field__c)}" />
</apex:outputText>
1

As stated earlier, Time does not have its own format method which may be the reason it's not working for outputText. You can try below and see if this works:

In the controller

Date d = Date.today();
Time t = Object__c.Time_Field__c // your time field
DateTime dt = DateTime.newInstance(d,t); 

You can now format this datetime(dt) in time specific format in the VF page.

<apex:outputText value="{0, Time, HH:mm:ss z}">
    <apex:param value="{!dt}"/> <!-- Date/Time field **dont forget to make it public get;set; property--> 
</apex:outputText>

You must log in to answer this question.

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