Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active August 17, 2018 14:35
Show Gist options
  • Select an option

  • Save SalesforceBobLightning/995773e2b213254c458005044eb23ad3 to your computer and use it in GitHub Desktop.

Select an option

Save SalesforceBobLightning/995773e2b213254c458005044eb23ad3 to your computer and use it in GitHub Desktop.
Custom Visualforce Component to display DateTime with User TimeZone

Visualforce Component to display DateTime using User TimeZone

Usage

<c:UserLocalDateTime value="{!NOW()}" format="EEE MMM d kk:mm:ss z yyyy" />
<apex:component access="global" controller="UserLocalDateTimeController">{!FormattedDatetime}
<apex:attribute assignTo="{!dateTimeValue}" description="The DateTime value to be rendered" name="value" type="DateTime"></apex:attribute>
<apex:attribute assignTo="{!dateTimeFormat}" description="The format to use for the DateTime value to be rendered" name="format" type="String"></apex:attribute>
</apex:component>
public class UserLocalDateTimeController {
public DateTime dateTimeValue { get; set; }
public String dateTimeFormat { get; set; }
public String getFormattedDatetime() {
if (dateTimeValue == null) return '';
TimeZone tz = UserInfo.getTimeZone();
return dateTimeValue.format(dateTimeFormat, tz.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment