function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jon-wujon-wu 

Format date/time from Salesforce object directly in element attribute

How can I format a date/time to the user's timezone within an attribute without creating a separate method to do it in the controller?

 

The outputText formatting won't work since I can't put outputText within an attribute since that's not valid XML.

 

For example:

 

<apex:repeat value="{!revisionHistory}" var="history">

   <a href="/{!history.CreatedById}" title="{!history.CreatedDate}">{!history.Name}</a>

</apex:repeat>

 

I want something like the above but I want title to be formatted date AND time in the user's local timezone. It appears you can do this with the date via forumlas but I can't find a way to do this with the date and time without writing a special getter which isn't ideal.

Shashikant SharmaShashikant Sharma

Do one thing Use

 

Datetime.format in your controller and assign it's return vaue to a string property and then use that string in the VFP.

Example : 

 

 

DateTime dt = DateTime.now();
System.debug('************** '+ dt.format());

 

 

 

In your case

 

String dtToDisplay = history.CreatedDate().format();
Use {!dtToDisplay} in VFP.

 

jon-wujon-wu

Thanks Shashikant. I know I can do this through the controller but I was wondering if there was a way to do it strictly in the view - that just seems a lot cleaner to me and easier to edit / deploy.

 

However, it seems like it may not be possible via VF or forumlas or other tricks especially with the time zone conversion (otherwise I could make a forumla to cut up the time as needed).

Shashikant SharmaShashikant Sharma

That right mate, Only way tha seems to solve this issue is to use controller.