Datetime on Visualforce Page

Whenever we are working with datetime on visualforce page, we may face some issue either with its formatting or its value not coming correctly. So basically the formatting of date depends on the locale of the user, and second thing which is its value depends on the timezone.

In the salesforce database each datetime value is stored in GMT, and when this is shown on the detail page it is adjusted with the current user’s timezone or offset. But sometime we developer create custom visualforce pages to fulfil different different business requirements. Here I come up with the all possible use cases and solution of showing Datetime on visualforce page with their pros and cons.

Datetime on Visualforce Page
  • Datetime Format (Showing DD/MM/YYYY, showing time only, showing year only, showing week day etc).
  • Datetime Value (Either in GMT or in current user timezone).
Method/Approach Taken Is Format Customizable ? Is Current Timezone ?
apex:outputText – direct datetime value. NO NO
apex:outputText – formatted using apex:param. YES NO
apex:outputText – with one space in value(Trick). NO YES
apex:outputText – (Offset is added on page). YES(But in controller) YES
apex:outputText – Showing a String containing formatted Datetime from controller. YES YES
apex:outputText – (Offset is added from controller). YES YES
apex:outputField – (No Customization can be done with format). NO YES
Datetime on visualforce page
Datetime on Visualforce page
Source Code

Visualforce page Code

Apex controller

Conclusion

For Different format see Datetime format Patterns.

So I have shared my finding on the datetime on visualforce page, I hope that this will help developers. Please comment if you need further explanation or have any feedback.