By default, datetime fields rendered in Visualforce email templates display in GMT timezone, which may not match the recipient's local timezone.
The solution uses a custom Apex controller class, a Visualforce component, and a Visualforce email template that embeds the component.
To format a datetime field in a specific timezone within a Visualforce email template, follow these steps:
Step 1: Create an Apex Controller Class
Create an Apex controller class that accepts a DateTime value and an optional format string, and returns the DateTime formatted in a specified timezone using Apex's DateTime.format(String, String) method.
In Lightning: Go to Setup | Custom Code | Apex Classes | New. In Classic: Go to Setup | App Setup | Develop | Apex Classes | New.
The controller class below (controller_formatted_datetime) reads a datetime value and an optional format string from the component's attribute tags, then returns the formatted datetime. To specify a different timezone, replace 'PST' with the desired timezone identifier (e.g., 'IST', 'CST', 'America/New_York'):
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
}
}
}
}
Step 2: Create a Visualforce Component
Create a Visualforce component that binds to the controller and exposes the datetime value and format string as input attributes. This component can then be embedded in an email template.
In Lightning: Go to Setup | Custom Code | Visualforce Components. In Classic: Go to Setup | App Setup | Develop | Components.
The following component code (name: VFEmailTempComp) binds to the controller class and exposes two attributes — the DateTime value to render and an optional format string:
<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>
</apex:component>
Create a Visualforce email template and embed the component. Pass the desired datetime value and format string as attribute values.
In Lightning: Go to Setup | Email | Classic Email Templates | New Template | Visualforce. In Classic: Go to Setup | Administration Setup | Communication Templates | Email Templates.
The following email template code embeds the VFEmailTempComp component and uses the NOW() function to display the current time formatted in PST timezone. Adjust the date_time_format attribute to match your desired format pattern:
<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" />
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
To use a different timezone, update the controller_formatted_datetime class and replace 'PST' with your required timezone string:
return date_time.format(defined_format,'<Your Time Zone>'); // replace <Your Time Zone> with the time zone which you need the current time in.
Note: This is a sample code and needs to be customized according to your specific business need.
000385603

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.