Find your content:

Search form

You are here

Visualforce Locale specific date format

 
Share

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.


Attribution to: Daniel Blackhall

Possible Suggestion/Solution #1

If you can use the Salesforce outputField component instead of outputText, it will automatically adjust to the locale of the context user.

If you have a custom controller or extension on your page, the instance method format() on the Date primitive will return the date as a string formatted in the user's locale.

Opportunity.CloseDate.format()

Attribution to: Rob Scott

Possible Suggestion/Solution #2

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 to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/476

My Block Status

My Block Content