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
paul-lmipaul-lmi 

Is there a better way to format this date in VF?

I'm grabbing a datetime from a field binding in VF and I need to format it like YYYY-DD-MM .  The problem is, Salesforce will format as a single digit day and month instead of retaining the leading 0.

 

Is this the most efficient way to overcome that?

 

 

{!(TEXT(YEAR(d.lastpublisheddate)) & "-" & IF(LEN(TEXT(MONTH(d.lastpublisheddate)))==2,TEXT(MONTH(d.lastpublisheddate)),"0" & TEXT(MONTH(d.lastpublisheddate))) & "-" & IF(LEN(TEXT(DAY(d.lastpublisheddate)))==2,TEXT(DAY(d.lastpublisheddate)),"0" & TEXT(DAY(d.lastpublisheddate))))}

 For various reasons, I don't want to do this in Apex directly, mostly due to shared code that'd get exponentially more complicated if I was to do this transformation in Apex.

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

<apex:outputText> doesn't generate a <span> unless you specify CSS along with the tag.  Best, Steve.

All Answers

SteveBowerSteveBower

Presuming you're trying to display this on a VF page...   (otherwise, what are you trying to do with it?  :-)  )  Best, Steve.

 

   <apex:outputText value="The last published date is: {0,date,yyyy.MM.dd}">
       <apex:param value="{!d.lastpublisheddate}" />
   </apex:outputText>
paul-lmipaul-lmi

It's for outputing XML, so I can't use anything that adds span tags, unfortunately.

SteveBowerSteveBower

<apex:outputText> doesn't generate a <span> unless you specify CSS along with the tag.  Best, Steve.

This was selected as the best answer
paul-lmipaul-lmi

ah, i wasn't aware of that.  thanks for the insight Steve.