Pages

Tuesday, January 3, 2012

VF Templates: Formatted Merge Values (dates)

Just like in the previous post, another type of field that you might not receive your expected data format are date fields.
My record said: 

This is the VF code I was using:
{!relatedTo.myDateField__c}

The resulting text displayed in an email was:

That's not too pretty.
For date fields, you can extract individual pieces of the date, including the month, year, and date.  Once you pull those pieces out, you can separate them with a forward slash (“/”) and then it will look more like the date field you were hoping for.

I'm hoping for something that looks like "1/3/2012."

To retrieve the "1" for the month:

{!month(relatedto.Distribution_Form_Received_Date__c)}

To retrieve the "3" for the date:


{!day(relatedto.Distribution_Form_Received_Date__c)}


To retrieve the year "2012":

{!year(relatedto.Distribution_Form_Received_Date__c)}


Put them all together with those forward slashes:
{!month(relatedto.Distribution_Form_Received_Date__c)}/{!day(relatedto.Distribution_Form_Received_Date__c)}/{!year(relatedto.Distribution_Form_Received_Date__c)}


With that little bit of magic, your end result will be: