Forcing Timezone with Visualforce Data and Time

Some months ago I had a project where I needed to show what date and time an order was processed in Eastern regardless of who submitted the order and their related timezone. Google will quickly show the light for how to display date/time using the timezone set on the user profile or GMT. So the last time I worked this some months ago I found a tip (which for the life of me I cannot find again) that showed how to work around this:

<apex:outputText value="{0,date,MMMM d, yyyy @ h:mm a}">
<apex:param value="{!RelatedTo.Processed_DateTime__c - 0.166666667}" />
</apex:outputText>

 
All pretty standard stuff. So the magic sauce when dealing with Daylight Savings Time (EST vs. EDT), you take your GMT date/time field and subtract either 0.166666667 for EDT (-4 hours) or 0.2 for EST (-5 hours) depending on the time of year. Or basically one (1) divided by the number of hours offset. 1/4 = .166~ and 1/5 = .2 respectfully.

Doesn’t seem that interesting but if nothing else making a note for the next time that I need to fix this…by then I’ll take the time to write some logic to take in to account EDT and EST!

Also just in case you want to show GMT or the timezone set by the user, here is a super cool tip:

<apex:outputText value="{!NOW()}" />  - it will show GMT time, but if you use
<apex:outputText value=" {!NOW()}" /> (note that blank space has been added before the left brace) then you'll see time in the local format

 
This comes by the way of this page: apex:outputtext should support formatting of date/time in local time zone

 

2 Replies to “Forcing Timezone with Visualforce Data and Time”

  1. Works great, thanks. PST looms! Look forward to hearing about the code so that I do not have to go back and change it at the beginning and end of each summer here in California. Thanks again.

  2. For me , it was throwing the below error .
    Error: The value attribute on is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.

Leave a Reply to Subbarao Cancel reply

Your email address will not be published. Required fields are marked *