Get dateformat based on Locale

i am trying to fetch the date format for the logged-in User in javascript on visualforce page. Usually I used to get this data by using:

window.UserContext.dateFormat

Expected Output should be: "dd.mm.YYYY"

but this seems to return blank to me on my page. Could I get this from SOQL query somehow or some other util method?

Replay

seems like I answered this once before but I can't find it (so, reproducing answer again ... from my Util.cls)

private static Map<String,String>                       localeToDateTimeFmtMap;             // Singleton, remember when first referenced
//  -------------------------------------------------------
//  getUserDateFormat: e.g. M/d/yyyy
//  -------------------------------------------------------
public static String getUserDateFormat() {
    String      userLocale  = UserInfo.getLocale();
    getLocaleToDateTimeFmtMap();
    if (!localeToDateTimeFmtMap.containsKey(userLocale))    return 'yyyy-mm-dd';
    return localeToDateTimeFmtMap.get(userLocale).substringBefore(' ');
}

//  -------------------------------------------------------
//  getUserDateTimeFormat: e.g. M/d/yyyy hh.mm.ss
//  -------------------------------------------------------
public static String getUserDateTimeFormat() {
    String      userLocale  = UserInfo.getLocale();
    getLocaleToDateTimeFmtMap();
    if (!localeToDateTimeFmtMap.containsKey(userLocale))    return 'yyyy-mm-dd hh.mm.ss';
    return localeToDateTimeFmtMap.get(userLocale);
}

//  -------------------------------------------------------
//  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;
}

If you can use the Salesforce outputField component, 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()

Category: apex Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.192 (s). 12 q(s)