Visualforce Locale specific date format

How can I go about displaying a date that is formatted to a users locale, for example me and my Australian friends use the format dd/MM/yyyy, while the Americans amoung us use MM/dd/yyyy. I know I can format a date in visualforce like so

<apex:outputText value="{0,date,dd/MM/yyyy}">
    <apex:param value="{!myDateValue}" /> 
</apex:outputText>

but how can I use the salesforce locale, There is a complete list but how can I use these on my page?

I have found a very comprehensive solution here, but I was hoping someone new of an inbuilt, or more elegant way.

Answer

The easiest way to do this might be to utilise the ‘named’ date formats that are available, for example, short and long:

<apex:outputText value="{0,date,short}">
    <apex:param value="{!Opportunity.CloseDate}" /> 
</apex:outputText>

<apex:outputText value="{0,date,long}">
    <apex:param value="{!Opportunity.CloseDate}" /> 
</apex:outputText>

BUG ALERT

While official documentation hints that message formats respect locale, currently the named messages formats (short, medium, and long) always output in US date format (month then day) or possibly the locale of your instance. A support request was logged and R&D replies that this won’t work and will never work

This is “working as designed.” OutputText has always been un-respectful of user locale. For user locale specific data you need to use OutputField. While addressing this may fix the issue for you the rest of our customers have been using it this way from the begin. While MessageFormat.java does talking about using locale, it uses the locale of the localhost only, not the running users.

Attribution
Source : Link , Question Author : Daniel Blackhall , Answer Author : Ralph Callaway

Leave a Comment