[SalesForce] Formating the new time field in Visualforce

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>

Best Answer

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}" />
Related Topic