December 12, 2019

How to show DateTime in Visualforce page in User's Time Zone instead of GMT in Salesforce?

Sample Code:


  1. <apex:page StandardController="Opportunity">  
  2.   
  3.     GMT:   
  4.     <apex:outputText value=" {0,date,M/d/yyy h:mm a}">  
  5.         <apex:param value="{!Opportunity.LastModifiedDate}"/>  
  6.     </apex:outputText>  
  7.     <br/>  
  8.     User Time Zone:   
  9.     <apex:outputField value="{!Opportunity.LastModifiedDate}"/>  
  10.       
  11. </apex:page>  

Output:



For using apex:outputText tag use


  1. <apex:page>  
  2.       
  3.     <table>  
  4.         <tr>  
  5.             <td><apex:outputText value="User Local Time Zone time is: {! NOW() }" /></td>  
  6.         </tr>  
  7.         <tr>  
  8.             <td><apex:outputText value="GMT Time Zone time is: {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}"><apex:param value="{! NOW() }" /></apex:outputText></td>  
  9.         </tr>  
  10.     </table>  
  11.   
  12. </apex:page>  

Note:
<apex:outputText value="{!NOW()}" />  - it will show GMT time.
<apex:outputText value=" {!NOW()}" /> - it will show time in your local time zone (note that blank space has been added before the left brace).

No comments:

Post a Comment