September 5, 2011

Formatting That Date — One Solution to "apex:outputtext is not in a valid format"

So you want to put a date on a page, you know about using the value="{0,date,short}" Java style syntax, and you know to put your field into an <apex:param> (don't panic if you don't, the code is below), but when you try and use a datetime field you get this rather infuriating error:

Save 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.

The easiest way to fix this is to make use of the built in Visualforce functions DATEVALUE() and DATETIMEVALUE() as necessary and you should be sorted.

<apex:outputText value="{0,date,dd/MM/yyyy}"> 
    <apex:param value="{!DATEVALUE(m_dtimSomeVar)}"/> 
</apex:outputText>