[SalesForce] Timezone issue with Datetime Field [again ;-)]

I probably saw this question 100 times, but still don't understand.
So basically I want to display only the Time from a datetime field but not the Date. However I always get the GMT time. I know I could create a wrapper and format my datetimes in the controller. But why should I create a wrapper class only to display datetimes. I made some tests and here is what I got :

//not good, display GMT time
<apex:outputText value="{0,date,HH:mm}">
     <apex:param value="{!item.Inspection_Date_Time__c}" name="p1" />
</apex:outputText>

//not good, display GMT time
<apex:outputText value="{0,number,00}:{1,number,00}">
    <apex:param value="{!item.Inspection_Date_Time__c.time.hours}" name="p1" />
    <apex:param value="{!item.Inspection_Date_Time__c.time.minutes}" name="p2" />
</apex:outputText>

//time is correct but I want only the time not the date
<apex:outputText value="* {!item.Inspection_Date_Time__c}" />

//display GMT time, and what's the difference with the previous line ?
<apex:outputText value="{!item.Inspection_Date_Time__c}" />

//time is correct but I want only the time not the date
<apex:outputField value="{!item.Inspection_Date_Time__c}" />

Anyone found a solution for that(except via the controller)?

Cheers,

David

Best Answer

Use Apex to get User's TimeZone and offset time. e.g.

<apex:page controller="TimeZoneController">
    <apex:outputText value="{0,date,dd/MM/yyyy HH:mm:ss}">
        <apex:param value="{!NOW()+offset}"/>
    </apex:outputText>
</apex:page>

public class TimeZoneController{
    public Double offset{get{
        TimeZone tz = UserInfo.getTimeZone();
        //Milliseconds to Day
        return tz.getOffset(DateTime.now()) / (1000 * 3600 * 24.0);
    }}
    public TimeZoneController(){
    }
}

[Reference] https://www.xgeek.net/salesforce/display-datetime-with-timezone-in-visualforce-page/

Related Topic