Formatting Time in Salesforce
While working on a Salesforce project, I found that I needed to format a time field from an object into VisualForce email templates and a couple VisualForce pages. The VisualForce page wasn't very difficult as I could just pass an Extension to the <apex:page />
tag, but Email Templates don't support Extensions.
So instead, I had to rely on a component. Without further ado, here's the component I used to format time:
First the class that does the heavy lifting:
public class FormatTime {
public Time rawTime {get;set;}
// I needed to manipulate the time slightly sometimes
public Integer addedMinutes {get;set;} { addedMinutes = 0; }
public string getFormattedTime() {
Time t = rawTime.addMinutes(addedMinutes);
Integer hour = t.hour();
Integer minute = t.minute();
String ampm = 'AM';
if (hour >= 12) {
ampm = 'PM';
if (hour > 12) { hour = hour - 12; }
}
return hour.format().leftPad(2, '0') + ':' + minute.format().leftPad(2, '0') + ' ' + ampm;
}
}
And the component:
<apex:component controller="FormatTime" access="global">
<apex:attribute name="time" type="Time" assignTo="{!rawTime}" description="Time to format" />
<apex:attribute name="add" type="Integer" assignTo="{!addedMinutes}" default="0" description="Minutes to add to the time passed in" />
{!formattedTime}
</apex:component>
Simple enough. To use it call the <c:FormatTime />
tag in your VisualForce code:
<!-- Will output the time in the format of 09:33 PM -->
<c:FormatTime time="{!Program__c.End_Time__c}" />
<!-- Will subtract 15 minutes from the time and output it -->
<c:FormatTime time="{!Program__c.End_Time__c}" add="-15" /