Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

Visualforce メールテンプレートの日時項目のタイムゾーンの書式設定

公開日: May 3, 2026
説明
Visualforce メールテンプレートのタイムゾーンが常に GMT で、あまり便利ではありません。自分のタイムゾーンを使用するにはどうすればよいですか?
解決策
メールテンプレートで、さまざまなタイムゾーンでの日付を書式設定するには、次の手順に従います。
 
1. まず、コンポーネントのコントローラクラスを作成します。

Classicの場合: [設定] | [開発] | [Apex クラス] | [新規] に移動します。

Lightningの場合: [設定] | [カスタムコード] | [Apex クラス] | [新規] に移動します。
 
Formatted_DateTime_Controller クラスのコード
public class controller_formatted_datetime
{
public DateTime date_time { get; set; } // コンポーネントの属性タグから日付/時間型の値を読み取るプロパティ
public String defined_format { get; set;} // コンポーネントの属性タグから文字列型の値を読み取るプロパティ
public String getFormattedDatetime(){
if (date_time == null) 

{
return ''; 
}
else { 

if (defined_format == null) {
return date_time.format(); // ユーザの場所およびタイムゾーンで完全な日付/時間型を返します
}
else 
{ 
return date_time.format(defined_format,'PST');  // IST、CST などのタイムゾーンを指定します
}

}

}

}
2.コンポーネントを作成します。

Classicの場合: [設定] | [開発] | [Visualforce コンポーネント] に移動します。

Lightningの場合:  [設定] | [カスタムコード] | [Visualforce コンポーネント] に移動します。

 
VF コンポーネントのコード (名前: 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>
</apex:component>

3.メールテンプレートを作成し、コンポーネントを埋め込みます。

Classicの場合: [設定] | [コミュニケーションテンプレート] | [メールテンプレート] に移動します。

Lightningの場合:  [設定] | [メール] | [Classic メールテンプレート] | [新規テンプレート] | [Visualforce] に移動します。
 
メールテンプレートのコード
<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>

これによって、PST タイムゾーンでの現在の時刻が表示されます。

Controller_Formatted_Datetime のコードでタイムゾーン (IST、CST など) を指定することによって、任意のタイムゾーンでの現在の時刻を取得できます。
return date_time.format(defined_format,'<Your Time Zone>'); // <あなたのタイムゾーン> を現在必要とするタイムゾーンに置き換えます


 
ナレッジ記事番号

000385603

 
読み込み中
Salesforce Help | Article