I just ran into a quirk with displaying DateTime fields in visualforce. Apparently, the tag does not convert the date into the current user’s timezone but rather renders it in GMT (the value as salesforce stores it internally).
Problem:
Renders:
Tue May 27 02:50:00 GMT 2014
I did some googling and came across this request on the IdeaExchange as well as various workarounds from various blogs and forums, some were as complex as creating custom component and controller to do the timezone conversion in apex. Creating a custom component seems like too much trouble to me to just get a date to display correctly. Thankfully, the simplest solution was just to add whitespace to the front of the value expression and voila!
Workaround:
Renders:
5/26/2014 9:50 PM
I don’t know why adding the whitespace makes the format display correctly, but it does, and until it doesn’t then no need for extra code to write/test/maintain.
But don’t forget, unless you actually need to use tag for custom message formatting, you can always use tag and it displays the date in the current user’s timezone correctly (no hack required!).
(Update 5/25/2015)
Chirag Mehta pointed out in the comments of this post that this workaround does not work when using using tag with nested element, such as when wanting to use a custom date format. As I originally mentioned, if you need to use custom message formatting then you’ll need to resort to a more complex apex solution (e.g. generate the appropriate display string in apex and display in visualforce).
Problem:
Renders:
5/27/2014 02:50 AM GMT
Space doesn’t seem to work when its outputtext with param.
LikeLike
Hi Chirag,
Would you mind sharing your code snippet? Remember, this solution is really just a hack and may not work in all cases. If all else fails, you may have to resort to generating the display text in apex code using DateTime.format method.
Thanks
LikeLike
Hi Chirag,
I’ve updated the post to be more explicit that using
<apex:outputText>
with nested<apex:param>
tag isn’t influenced by the “whitespace hack” and that likely a custom apex solution is necessary when wanting custom message formats.Thanks
LikeLike
Thank you 🙂 Really appreciate your prompt response here.
LikeLike
Workaround for me was to format date in the class:
dateTime tempDateTime = opp.Target_Date__c;
// adding a timezone parameter to prevent date from being displayed incorrectly
targetDate = tempDateTime.format(‘MMM dd, yyyy’,’GMT’);
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm#apex_System_Datetime_format_3
LikeLike
Hi Oleksiy,
Thanks for the feedback. Looks like you needed custom formatting like Chirag needed. Unfortunately, as you’ve discovered, formatting in Apex seems to be only workaround in that case. My “single space” trick will simply format the date in the current user’s locale.
Thanks,
Doug
LikeLike
Hi Doug
what can we do for Custom email templates so that they show User’s timezone in emails.
Regards
jyoti
LikeLike
Hi Jyoti, what have you tried so far? Can you share a gist to your code snippet? Thanks!
LikeLike