[SalesForce] Getting Desired DateTime format in Visualforce Email Template

I'm currently making a making a visualforce email template and I'm having a problem with displaying the desired output format.

I have a field, Opportunity.Start_DateTime__c with DatetTime format.

Ex. 3/02/2017 9:55 PM

How do I display it as Friday 3rd February 2017 and 03 Feb 2017 9:55 PM?

So far I tried these:

input1 : <apex:outputText value="{!cx.Opportunity.Start_Date_Time__c}"/>

output1: Fri Feb 03 10:55:00 GMT 2017 (don't need the GST one)

input2 : <apex:outputText value="{!DATETIMEVALUE(cx.Opportunity.Start_Date_Time__c)}"/>

output2: 3/02/2017 9:55 PM (correct data / incorrect format)

input3:

<apex:outputText value="- {0,date,dd MMM yyyy hh:mm a}"> 
                            <apex:param value="{!DATETIMEVALUE(cx.Opportunity.Start_Date_Time__c)}" />
                        </apex:outputText>

output3: 03 Feb 2017 10:55 AM (correct format but don't need GST one)

Really need this…Thanks in advance !

Best Answer

I have created some Visualforce markup to demonstrate how we can display different DateTime formats for the Contact CreatedDate. Documenation on the formatting can be found at Salesforces date formatting documentation.

Here is the entire markup being used and its results showing:

<apex:page standardController="Contact">
    <apex:pageBlock>
        <apex:pageBlockSection>    
            <apex:pageBlockSectionItem>
                <apex:outputText value="{0, date,EEEE d MMMM yyyy}">
                    <apex:param value="{!contact.CreatedDate}" /> 
                </apex:outputText>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem>
                <apex:outputText value="{0, date,d MMMM yyyy HH:mm a}">
                    <apex:param value="{!contact.CreatedDate}" /> 
                </apex:outputText>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Varioous DateTime formatting

Using "{0, date,EEEE d MMMM yyyy}" we can display day in text / day / month / year. EEEE is taken from the Java Docs for SimpleDateFormat and provides us with the Day unabbreviated. If you need the day abbreviated please use EEE as explained in those docs.

The second formatting technique we use eliminates the displaying of the day in text and rearranges the date format. We use the letter a to display AM/PM as documented in Pauls SF Blog on Formatting Time in APEX

Hope this helps.

Related Topic