2

So I know I can just render a field with its default formatting using outputField:

<apex:outputField value="{!MyObject__c.MyDate__c}" />

If I try to render the same field using outputText I don't get the field formatting but the field just "as is":

<apex:outputText value="My value: {0, date, yyyy-MM-dd}">
    <apex:param value="{!MyObject__c.MyDate__c}" />
</apex:outputText>

Since the formatting has to be dependent on the settings of the user it wouldn't be an option to explicitly state the format for this field. Is there any way I can get the default field formatting just like with outputField?

3
  • so you want to format the date per the user's locale?
    – cropredy
    Mar 23, 2016 at 0:07
  • I believe this requires a controller that looks up the user's locale and maps to a datetime format that you can reference in a custom component
    – cropredy
    Mar 23, 2016 at 0:14
  • Yes. That's exactly what I want. And not just for dates but for every possible type.
    – Semmel
    Mar 26, 2016 at 11:36

1 Answer 1

3

Here is an outline of a solution (for dates and datetimes). Other data types I leave as an exercise for the reader

You'll need a VF Component Controller

public with sharing class UserLocale {

    public Object valueToDisplayInLocale {get; set;}

    public String localePattern {
        get {
            if (this.valueToDisplayInLocale instanceof Date)
                return Util.getLocaleToDateTimeFmtMap().get(UserInfo.getLocale()).substringBefore(' ');
            else
            if (this.valueToDisplayInLocale instanceof DateTime)
                return Util.getLocaleToDateTimeFmtMap().get(UserInfo.getLocale());
            return null;    
        }
    }

    public Boolean getIsDate() {return  this.valueToDisplayInLocale instanceof Date ? true : false;}
    public Boolean getIsDateTime() {return  this.valueToDisplayInLocale instanceof DateTime ? true : false;}
}

You'll need a VF custom component

<apex:component controller="UserLocale">

    <apex:attribute name="value" type="Object" description="value to format in userLocale" assignto="{!valueToDisplayInLocale}" required="true"/>


    <apex:outputText value="My date value: {0, date, {!localePattern}}"
                     rendered="{!isDate || isDateTime}">
        <apex:param value="{!value}" />
    </apex:outputText>

</apex:component>

You'll need this Utility Method

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

Sample VF page

<apex:page standardController="Account">
    <c:userLocale value="{!Account.CreatedDate}"/>
</apex:page>

Results (locale of running user = English UK)

My date value: 24/01/2016 23:44

Results (locale of running user = English US)

My date value: 1/24/2016 11:44 PM

Before you ask "Do you have the number formats already coded up?" The answer is no; I had only done dates before on some project as the orgs I worked on used a consistent US currency formatting across all locales. Others may have done the necessary work for the number formats

3
  • This is excellent. Bookmarked the post :)
    – javanoob
    Mar 28, 2016 at 15:13
  • Wow! To be honest - I didn't need a solution. I was just curious if you had to do it the hard way. But I guess this will help others with similar problems or who need a copy & paste solution. Thank you so much for you efforts. :-)
    – Semmel
    Apr 5, 2016 at 18:15
  • tx; I'm motivated to extend my utility class to handle number formats too now. watch this space!
    – cropredy
    Apr 6, 2016 at 3:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .