2 Step method to format Time (HH:MM:SS) in Salesforce using VisualForce Page (without Controller class or Methods)
After much research on the internet, we gave up searching for articles to help with Time/Date formatting In Salesforce. Our internal team put its time together on the whiteboard and arrived at the solution, more quickly than the time we took to research.
In this White Paper we intend to show you an easy way to display hours, minutes, and seconds just the way you want them using Salesforce Apex / VisualForce.
I am sure most of us try to get the answer in google with the Error Codes to begin with. So, here we go with the most notorious error that Salesforce Developers encounter while meddling with time, hour and date formats.
Error:
“The value attribute on <apex:outputText> is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.”
This error mostly means there’s a problem with how you’re trying to show time. This can be solved by using the <apex:outputText> tag with <apex:param>. Very Importantly Time formatting can be achieved without any traditional Controller Classes or Methods.
Salesforce VisualForce Code to solve the Time formatting issue:
<apex:outputText value=”{0,number,00}”>
<apex:param value=”{!Hour(Object__c.time__c)}” />
</apex:outputText>:
<apex:outputText value=”{0,number,00}”>
<apex:param value=”{!Minute(Object__c.time__c)}” />
</apex:outputText>:
<apex:outputText value=”{0,number,00}”>
<apex:param value=”{!Second(Object__c.time__c)}” />
</apex:outputText>
The above VF Page code is a way to display the hour, minute, and second components of the “Time__c” field in a specific format on a Salesforce Apex page.
Here’s a breakdown of each element:
- <apex:outputText value=”{0,number,00}”>: This portion formats a numeric value ({0}) with a leading zero (00) if necessary. It sets the stage for presenting the time components in the desired format.
- <apex:param value=”{!Hour(Object__c.Time__c)}” />: This segment retrieves the hour component from the “Time__c” field of the “Object__c” object. It ensures that the hour is accurately fetched and prepared for display.
- <apex:param value=”{!Minute(Object__c.Time__c)}” />: Similar to the hour component, this section retrieves the minute component from the “Time__c” field. It ensures that minutes are seamlessly integrated into the time display.
- <apex:param value=”{!Second(Object__c.Time__c)}” />: Lastly, this part fetches the second component from the “Time__c” field. It completes the trio of time components, ensuring that seconds are elegantly showcased alongside hours and minutes.
With this concise yet powerful code snippet, you can effortlessly display the hour, minute, and second components of the submission time in a format like “HH:MM:SS,” complete with leading zeros if needed. This functionality enhances the visual appeal and readability of your VF page, providing users with clear and concise time information.
Comment on this page or write to us with more observations and similar errors you are facing. Let’s try to solve them together.
#displaying time components in Salesforce Apex VF page
#Low code solution for Salesforce time, hour and date formatting.
#Salesforce resources for Date and Time Formatting.