Print this page

How Do You Control The Timezone Of A DateTime Field In A Visualforce Email Template

Knowledge Article Number 000066915
Description The time zone is a visualforce email template is always GMT which is not very useful. How can I use my timezone?
Resolution In order to format the date in various TimeZone in Email Template, Please follow the steps:
1. First create a Controller class for the Component
Setup|App Setup|Develop|Apex Classes|New
Formatted_DateTime_Controller Class Code
public class controller_formatted_datetime
public DateTime date_time { get; set; } //property that reads the datetime value from component attribute tag
public String defined_format { get; set;} //property that reads the string value from component attribute tag
public String getFormattedDatetime()
if (date_time == null) {return ''; }
else { if (defined_format == null) {
return date_time.format(); //return the full date/time in user's locale and time zone
else { return date_time.format(defined_format,'PST');  //Specify Time zone like IST,CST
2. Create a Component
Setup|App Setup|Develop|Components
VF Component Code (Name: VFEmailTempComp)
<apex:component access="global" controller="controller_formatted_datetime">{!FormattedDatetime}
<apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered" name="date_time_value" type="DateTime"></apex:attribute>
<apex:attribute assignTo="{!defined_format}" description="The optional format to use for the DateTime value to be rendered" name="date_time_format" type="String"></apex:attribute>
3. Create an Email Template and embed the component.
Setup|Administration Setup|Communication Templates|Email Templates
Email Template Code
<messaging:emailTemplate subject="Testing DateTime Format" recipientType="Contact" >
<messaging:plainTextEmailBody >
Formatted: <c:VFEmailTempComp date_time_value="{!NOW()}" date_time_format="EEE MMM d kk:mm:ss z yyyy" />
It will display current time in PST timezone.

promote demote