February 12, 2013

How to set date format for apex:outputText?

For MM/DD/YYYY format:


  1. <apex:outputText value="{0,date,MM/dd/yyyy}" >  
  2.     <apex:param value="{!dateField}"/>  
  3. </apex:outputText>    

Sample Code:

Visualforce page:

  1. <apex:page controller="Sample" >  
  2.   
  3.     <apex:form >           
  4.   
  5.          <apex:outputText value="{0,date,dd'/'MM'/'yyyy}" >  
  6.   
  7.              <apex:param value="{!temp}"/>  
  8.   
  9.          </apex:outputText>  
  10.   
  11.     </apex:form>  
  12.   
  13. </apex:page>   

Apex Controller:


  1. public with Sharing class Sample {  
  2.       
  3.     public Date gettemp() {  
  4.   
  5.         Date d = date.ValueOf('2013-12-10');  
  6.         return d;  
  7.   
  8.     }  
  9.   
  10.     public List < Member__c > mem {get;set;}      
  11.          
  12.     public sample() {}  
  13.   
  14. }  

Output:



Note: Make sure 'MM' is alone in capital letters.

4 comments:

  1. How can i sort it as in dates?

    ReplyDelete
    Replies
    1. This example is just to show/display date format using outputText.

      Delete
  2. How we can show only the date from datetime, if we have multiple fields in the value used.

    ReplyDelete
    Replies
    1. You can use the same. Check this for example - https://www.infallibletechie.com/2021/04/date-and-datetime-formating-in.html

      Delete