Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
1  
Have you tried just using {0,date,short} or {0,date,long}? –  LaceySnr Aug 16 '12 at 1:39
    
I have not, I was not aware that was possible. This works perfectly, Thanks Matt! btw you should have posted this as an answer –  Daniel Blackhall Aug 16 '12 at 6:36
    
Fyi, this doesn't work for me. Regardless of the user's locale the short, medium, and long date formats are always in US format. –  Ralph Jan 10 '13 at 2:34
1  
@Ralph I don't know what reopening your question is going to do, It is obviously a Salesforce bug. Good work getting it to tier 3. It's strange because it used to work, so it may take less than 2-4 years to fix (revert whatever change was made internally). –  Daniel Blackhall Jan 26 '13 at 4:06
1  
@Ralph I finally got around to testing this again, and you're right it's not respecting the locale. It's also preposterous, how would displaying the correct date for the locale negatively affect customers! –  Daniel Blackhall Feb 27 '13 at 1:53

3 Answers 3

up vote 14 down vote accepted

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.

share|improve this answer
2  
there is also a medium (dd MMM, yyyy), which ended up being perfect for my purposes –  Daniel Blackhall Aug 16 '12 at 6:39
    
Fyi, this doesn't work for me. Regardless of what locale is specified for the user the date format is always a US date format. –  Ralph Jan 10 '13 at 2:33
    
@Ralph Did you double check that your locale is not US? Should be no reason for it not to work... –  LaceySnr Jan 10 '13 at 2:37
    
@LaceySnr for sure, I've got a test page with outputfield and outputtext. When I change my locale to 'English (United Kingdom)' the outputfield displays the date in dd/MM/yy, but the outputtext (using the {0, date, short}) displays MM/dd/yy, are you not experiencing the same thing? –  Ralph Jan 10 '13 at 2:46
3  
@LaceySnr fyi, logged a bug for this which is with Tier 3, so they should have a fix in 2-4 years –  Ralph Jan 26 '13 at 0:41

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()
share|improve this answer
    
Good answer, I only used the close date as an example for my snippet (which I have edited for clarity) –  Daniel Blackhall Aug 16 '12 at 6:37
    
You can still use format() in Apex if 'myDateValue' is of type Date - myDateValue.format(). –  Rob Scott Aug 20 '12 at 21:13
1  
To make outputField valuable in a wider range of scenarios, you could create a "Types" sObject, which would (among other things) have a Date field. In your controller you would use an instance of this sObject rather than a simple Apex Date value, and bind your outputField to this sObject field. You could wrap it all up in a custom component whose controller has an instance of your Types sObject; you would pass in the date value to the component with an assignTo your types sObject field, and use an outputField to render the value. –  Stephen Willcock Jan 28 '13 at 11:05

+1 On using If you can get away with accessing the field directly from an sobject, using the apex:outputField will dynamically render the date field according to the user's timezone and locale.

share|improve this answer
    
This is a repeat of an answer given a day before you offered it by @Rob Scott. On this site you can "vote" for answers you like or recommend rather than posting another answer saying "+1" –  Benj Aug 17 '12 at 20:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.