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.

Possible Duplicate:
Visualforce Locale specific date format

Is there anyway to get the <apex:outputtext> tag to respect a user's locale when printing out dates?

Example Controller:

public class Example {
  public Date dateVariables { get { return Date.today(); } }
}

Example Page:

<apex:page controller="Example">
  <apex:outputText value="{0, date, short}">
    <apex:param value="{!dateVariable}"/>
  </apex:outputText>
</apex:page>

Output is always MM/DD/YY, when I'd expect it to be DD/MM/YY for European user locale's. The java message format documentation gives the impression that this considers locale, but I'm guessing SFDC is not passing it to the message format class. Hopefully there is a better way to attack this.

Additional Details:

I have to use outputtext over outputfield for performance reasons since the page is an export of 1000s of records.

share|improve this question

marked as duplicate by Daniel Blackhall, Jon Hazan, Saariko Jan 11 '13 at 17:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
This is exact same scenario as : salesforce.stackexchange.com/questions/476/…. In case you are using a outputtext can you not use .format() on {!datevariable} –  rao Jan 8 '13 at 19:01
    
@rao thanks for the link, shame on me for not seeing that one already ;) –  Ralph Jan 10 '13 at 2:11
    
@rao, d'oh the other question implies this should work even though it doesn't –  Ralph Jan 10 '13 at 2:35
    
Ralph I think the way the post suggests is lil different. You are using a string to get the param value, but the post uses sobject.field_name. Are u pulling in a field from the standardcontroller to display as output text, if so can u try something like opportunity.name and see if it works ? –  rao Jan 10 '13 at 17:58
    
@rao, I'm not sure I follow about using the string to get the param value, added a controller to clarify. –  Ralph Jan 11 '13 at 0:25

2 Answers 2

http://www.interactiveties.com/b_locale_datetime.php

Just go through this blog there is a component built by the author and its really aweosme

public class controller_locale_formatted_datetime {

public DateTime date_time { get; set; } //property that reads the datetime value from the component attribute tag

//returns the properly formatted datetime value
public String getTimeZoneValue() {
    Map<String, String> mappedValues = new Map<String, String>(); //map for holding locale to datetime format
    mappedValues = MapValues(); //populate the map with all the locale specific datetime formats
    String user_locale = UserInfo.getLocale(); //grab the locale of the user
    String datetime_format = 'M/d/yyyy h:mm a'; //variable for the datetime format defaulted to the US format
    if (mappedValues.containsKey(user_locale)) { //if the map contains the correct datetime format
        datetime_format = mappedValues.get(user_locale); //grab the datetime format for the locale
    }
    String locale_formatted_date_time_value = date_time.format(datetime_format); //create a string with the proper format
    return locale_formatted_date_time_value; //return the string
}

//populate a map with locale values and corresponding datetime formats
private Map<String, String> MapValues() {
    Map<String, String> locale_map = new Map<String, String>(); //holds the locale to timedate formats
    locale_map.put('ar', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_AE', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_BH', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_JO', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_KW', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_LB', 'dd/MM/yyyy hh:mm a');
    locale_map.put('ar_SA', 'dd/MM/yyyy hh:mm a');
    locale_map.put('bg_BG', 'yyyy-M-d H:mm');
    locale_map.put('ca', 'dd/MM/yyyy HH:mm');
    locale_map.put('ca_ES', 'dd/MM/yyyy HH:mm');
    locale_map.put('ca_ES_EURO', 'dd/MM/yyyy HH:mm');
    locale_map.put('cs', 'd.M.yyyy H:mm');
    locale_map.put('cs_CZ', 'd.M.yyyy H:mm');
    locale_map.put('da', 'dd-MM-yyyy HH:mm');
    locale_map.put('da_DK', 'dd-MM-yyyy HH:mm');
    locale_map.put('de', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_AT', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_AT_EURO', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_CH', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_DE', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_DE_EURO', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_LU', 'dd.MM.yyyy HH:mm');
    locale_map.put('de_LU_EURO', 'dd.MM.yyyy HH:mm');
    locale_map.put('el_GR', 'd/M/yyyy h:mm a');
    locale_map.put('en_AU', 'd/MM/yyyy HH:mm');
    locale_map.put('en_B', 'M/d/yyyy h:mm a');
    locale_map.put('en_BM', 'M/d/yyyy h:mm a');
    locale_map.put('en_CA', 'dd/MM/yyyy h:mm a');
    locale_map.put('en_GB', 'dd/MM/yyyy HH:mm');
    locale_map.put('en_GH', 'M/d/yyyy h:mm a');
    locale_map.put('en_ID', 'M/d/yyyy h:mm a');
    locale_map.put('en_IE', 'dd/MM/yyyy HH:mm');
    locale_map.put('en_IE_EURO', 'dd/MM/yyyy HH:mm');
    locale_map.put('en_NZ', 'd/MM/yyyy HH:mm');
    locale_map.put('en_SG', 'M/d/yyyy h:mm a');
    locale_map.put('en_US', 'M/d/yyyy h:mm a');
    locale_map.put('en_ZA', 'yyyy/MM/dd hh:mm a');
    locale_map.put('es', 'd/MM/yyyy H:mm');
    locale_map.put('es_AR', 'dd/MM/yyyy HH:mm');
    locale_map.put('es_BO', 'dd-MM-yyyy hh:mm a');
    locale_map.put('es_CL', 'dd-MM-yyyy hh:mm a');
    locale_map.put('es_CO', 'd/MM/yyyy hh:mm a');
    locale_map.put('es_CR', 'dd/MM/yyyy hh:mm a');
    locale_map.put('es_EC', 'dd/MM/yyyy hh:mm a');
    locale_map.put('es_ES', 'd/MM/yyyy H:mm');
    locale_map.put('es_ES_EURO', 'd/MM/yyyy H:mm');
    locale_map.put('es_GT', 'd/MM/yyyy hh:mm a');
    locale_map.put('es_HN', 'MM-dd-yyyy hh:mm a');
    locale_map.put('es_MX', 'd/MM/yyyy hh:mm a');
    locale_map.put('es_PE', 'dd/MM/yyyy hh:mm a');
    locale_map.put('es_PR', 'MM-dd-yyyy hh:mm a');
    locale_map.put('es_PY', 'dd/MM/yyyy hh:mm a');
    locale_map.put('es_SV', 'MM-dd-yyyy hh:mm a');
    locale_map.put('es_UY', 'dd/MM/yyyy hh:mm a');
    locale_map.put('es_VE', 'dd/MM/yyyy hh:mm a');
    locale_map.put('et_EE', 'd.MM.yyyy H:mm');
    locale_map.put('fi', 'd.M.yyyy H:mm');
    locale_map.put('fi_FI', 'd.M.yyyy H:mm');
    locale_map.put('fi_FI_EURO', 'd.M.yyyy H:mm');
    locale_map.put('fr', 'dd/MM/yyyy HH:mm');
    locale_map.put('fr_BE', 'd/MM/yyyy H:mm');
    locale_map.put('fr_CA', 'yyyy-MM-dd HH:mm');
    locale_map.put('fr_CH', 'dd.MM.yyyy HH:mm');
    locale_map.put('fr_FR', 'dd/MM/yyyy HH:mm');
    locale_map.put('fr_FR_EURO', 'dd/MM/yyyy HH:mm');
    locale_map.put('fr_LU', 'dd/MM/yyyy HH:mm');
    locale_map.put('fr_MC', 'dd/MM/yyyy HH:mm');
    locale_map.put('hr_HR', 'yyyy.MM.dd HH:mm');
    locale_map.put('hu', 'yyyy.MM.dd. H:mm');
    locale_map.put('hy_AM', 'M/d/yyyy h:mm a');
    locale_map.put('is_IS', 'd.M.yyyy HH:mm');
    locale_map.put('it', 'dd/MM/yyyy H.mm');
    locale_map.put('it_CH', 'dd.MM.yyyy HH:mm');
    locale_map.put('it_IT', 'dd/MM/yyyy H.mm');
    locale_map.put('iw', 'HH:mm dd/MM/yyyy');
    locale_map.put('iw_IL', 'HH:mm dd/MM/yyyy');
    locale_map.put('ja', 'yyyy/MM/dd H:mm');
    locale_map.put('ja_JP', 'yyyy/MM/dd H:mm');
    locale_map.put('kk_KZ', 'M/d/yyyy h:mm a');
    locale_map.put('km_KH', 'M/d/yyyy h:mm a');
    locale_map.put('ko', 'yyyy. M. d a h:mm');
    locale_map.put('ko_KR', 'yyyy. M. d a h:mm');
    locale_map.put('lt_LT', 'yyyy.M.d HH.mm');
    locale_map.put('lv_LV', 'yyyy.d.M HH:mm');
    locale_map.put('ms_MY', 'dd/MM/yyyy h:mm a');
    locale_map.put('nl', 'd-M-yyyy H:mm');
    locale_map.put('nl_BE', 'd/MM/yyyy H:mm');
    locale_map.put('nl_NL', 'd-M-yyyy H:mm');
    locale_map.put('nl_SR', 'd-M-yyyy H:mm');
    locale_map.put('no', 'dd.MM.yyyy HH:mm');
    locale_map.put('no_NO', 'dd.MM.yyyy HH:mm');
    locale_map.put('pl', 'yyyy-MM-dd HH:mm');
    locale_map.put('pt', 'dd-MM-yyyy H:mm');
    locale_map.put('pt_AO', 'dd-MM-yyyy H:mm');
    locale_map.put('pt_BR', 'dd/MM/yyyy HH:mm');
    locale_map.put('pt_PT', 'dd-MM-yyyy H:mm');
    locale_map.put('ro_RO', 'dd.MM.yyyy HH:mm');
    locale_map.put('ru', 'dd.MM.yyyy H:mm');
    locale_map.put('sk_SK', 'd.M.yyyy H:mm');
    locale_map.put('sl_SI', 'd.M.y H:mm');
    locale_map.put('sv', 'yyyy-MM-dd HH:mm');
    locale_map.put('sv_SE', 'yyyy-MM-dd HH:mm');
    locale_map.put('th', 'M/d/yyyy h:mm a');
    locale_map.put('th_TH', 'd/M/yyyy, H:mm ?.');
    locale_map.put('tr', 'dd.MM.yyyy HH:mm');
    locale_map.put('ur_PK', 'M/d/yyyy h:mm a');
    locale_map.put('vi_VN', 'HH:mm dd/MM/yyyy');
    locale_map.put('zh', 'yyyy-M-d ah:mm');
    locale_map.put('zh_CN', 'yyyy-M-d ah:mm');
    locale_map.put('zh_HK', 'yyyy-M-d ah:mm');
    locale_map.put('zh_TW', 'yyyy/M/d a h:mm');
    return locale_map; //return the map
}

 }

Componet code

<apex:component access="global" controller="controller_locale_formatted_datetime"> 
<apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered    based upon the user's locale" name="date_time_value" type="DateTime"></apex:attribute>
 {!TimeZoneValue}
</apex:component>

An example on how to use this code

            <apex:pageBlockSection columns="2" title="Formatted System Information">
            <apex:outputField value="{!Account.CreatedById}">,      <c:locale_formatted_datetime date_time_value="{!Account.CreatedDate}"></c:locale_formatted_datetime></apex:outputField>
            <apex:outputField value="{!Account.LastModifiedById}">,   <c:locale_formatted_datetime date_time_value="{!Account.LastModifiedDate}"> </c:locale_formatted_datetime></apex:outputField>
        </apex:pageBlockSection>
share|improve this answer
    
thanks for the suggestion, unfortunately since I'm outputting a lot of records using this component would blow out the view state. Probably not a big deal, but would prefer not to have to update the component as new locales are added if there is anyway to make it happen dynamically –  Ralph Jan 10 '13 at 2:36

Ralph

I tried the following and it works as per user's Locale : the .format() should convert a date variable into a string which would be a date based on users locale enter image description here

I used the .format() on the date field and it worked as said @Rob Scott in the post : Visualforce Locale specific date format

Page :

<apex:page standardcontroller="account" extensions="outtext_controller">
<apex:outputText value="{!datevalue}"/>
</apex:page>

controller:

public with sharing class outtext_controller {
public string datevalue{get;set;}
    public outtext_controller(ApexPages.StandardController controller) {
    Account acc = [select id,createddate,date_val__c from account where id=:apexpages.currentpage().getparameters().get('id')];
    datevalue = acc.date_val__c.format();
    }
}
share|improve this answer
    
you're correct .format() respects locale, but the question is if there is anyway to get apex:outputText using the java message patter to work with locale –  Ralph Jan 11 '13 at 0:26
    
@Ralph Look at this post by techgerm tgerm.com/2011/01/message-format-string-format-apex.html This might give you some more insight if u did not stumble on this link. The link says that the format can take only string arguments and cannot take datatime, date, currency and number etc, –  rao Jan 11 '13 at 7:33
    
thanks for the link, i'm not using the apex format function, just the implied format functioned used in the apex:outputtext which can take datetime, date, currency, number formats. I've run into the .format() limitation, very annoying given it doesn't correspond with the functionality apex:outputText format strings. Of course, there are instance methods for Date (but no format string input) and DateTime (can pass it a format string) –  Ralph Jan 12 '13 at 2:26

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