I want to display all date fields in dd/mm/yy format. Currently it's displaying in dd/mm/yyyy format. (All fields are retrieved from a fieldset, the fieldset includes fields with different data types and when retrieving them all in an Apex:repeat, only the date fields should be displayed in dd/mm/yy format.) How can this be done? This is my VF code,

<apex:outputPanel layout="none" >
            <apex:repeat value="{!rsReportRows}" var="reportRow" >
                <!-- Risk Data -->
                <table cellpadding="7" style="border-top:1px solid black;border-left:1px solid black;border-right:1px solid black; border-bottom:1px solid black; width:100%;page-break-inside: avoid; ">
                        <td style="text-align:center;" colspan="{!parentObj.fieldApiNamesList.size}" ><b>Risk</b></td>
                    <tr style="page-break-inside:avoid;">
                        <apex:repeat value="{!parentObj.fieldLabelsList}" var="fieldLabel" >
                            <th style="font-size:{!pdfFontSize}pt;" ><apex:outputText value="{!fieldLabel}" /></th>
                        <apex:repeat value="{!parentObj.fieldApiNamesList}" var="field" >
                            <td >
                                <apex:outputField value="{!reportRow.risk[field]}" rendered="{!field != 'risk__residual_score_movement_direction__c'}" />
                                <apex:outputField value="{!reportRow.risk[field]}" rendered="{!field == 'risk__residual_score_movement_direction__c' && (CONTAINS(reportRow.risk[field],'Up') || CONTAINS(reportRow.risk[field],'Down') || CONTAINS(reportRow.risk[field],'Right'))}" />
  • 1
    Is there an easy way to tell Date fields apart from others? For example, do they all have 'Date' in the API name? Nov 4 '14 at 12:59
  • Yes they do. Is there any possible way to get this done ?
    – Tinkerbell
    Nov 4 '14 at 13:14

I don't understand all the rendered= logic in your VF, but the <apex:outputText> tag can format the date for you. Only render it if this field has "Date" in the API name.

<apex:outputText value="{0,date,dd/MM/yy}" rendered="{!(CONTAINS(field, 'Date'))}">
    <apex:param value="{!reportRow.risk[field]}" />

Of course, this is fragile, and if you make a new Date field without that word in the name, this will be broken. To keep better track of date fields, you would need to get Describe information on each field within the controller (maybe in a map) and keep track of which fields are Dates. That information would be used in the rendered= attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.