0

I have a custom object called Machine__c and i have a custom picklist field in it called Time_Zone__c. This Time_Zone__c field has the same picklist values as the Standard User object timezone field.

Based on this timezone field i am converting the datetime values to that particular timezone and i want to show the dates as per the standard salesforce date formats.

the date formats change based on the user locale. i want to utilize the same thing here.

please let me know if it is possible.

below is the code for converting the datetime to required timezone.

public static string getFormatedTZ( Datetime exOccr, string TZ) {
        String locale_formatted_date_time_value='';
        if(exOccr != null) {
          Map<String, String> mappedValues = new Map<String, String>(); 
          mappedValues = MapValues();
          String user_locale = UserInfo.getLocale();
          String datetime_format = 'MM/dd/yyyy h:mm a'; //default it to 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           
          }
          if(TZ != null){
            locale_formatted_date_time_value = exOccr.format(datetime_format,TZ.subString(TZ.lastIndexOf('(')+1, TZ.length()-1));
          }else{
            locale_formatted_date_time_value = exOccr.format();
          }
        }
        return locale_formatted_date_time_value; //return the string    

    }

Below is the MapValues map.

public static 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');     // arabic
      locale_map.put('ar_AE', 'dd/MM/yyyy hh:mm a');    // Arabic (United Arab Emirates)  
      locale_map.put('ar_BH', 'dd/MM/yyyy hh:mm a');    // Arabic (Bahrain)
      locale_map.put('ar_JO', 'dd/MM/yyyy hh:mm a');    // Arabic (Jordan)  
      locale_map.put('ar_KW', 'dd/MM/yyyy hh:mm a');    // Arabic (Kuwait)
      locale_map.put('ar_LB', 'dd/MM/yyyy hh:mm a');    // Arabic (Lebanon)
      locale_map.put('ar_SA', 'dd/MM/yyyy hh:mm a');    // Arabic (Saudi Arabia)
      locale_map.put('bg_BG', 'dd.MM.yyyy hh:mm');    //  Bulgarian (Bulgaria)
      locale_map.put('ca', 'dd/MM/yyyy HH:mm');     // Catalan
      locale_map.put('ca_ES', 'dd/MM/yyyy HH:mm');    // Catalan (Spain)
      locale_map.put('ca_ES_EURO', 'dd/MM/yyyy HH:mm'); // Catalan (Spain, Euro)
      locale_map.put('cs', 'dd.M.yyyy HH:mm');      // Czech 
      locale_map.put('cs_CZ', 'dd.M.yyyy HH:mm');     // Czech (Czech Republic)
      locale_map.put('da', 'dd-MM-yyyy HH:mm');     // Danish 
      locale_map.put('da_DK', 'dd-MM-yyyy HH:mm');    //Danish (Denmark)
      locale_map.put('de', 'dd.MM.yyyy HH:mm');     //German 
      locale_map.put('de_AT', 'dd.MM.yyyy HH:mm');    //German (Austria)
      locale_map.put('de_AT_EURO', 'dd.MM.yyyy HH:mm'); //German (Austria, Euro)
      locale_map.put('de_CH', 'dd.MM.yyyy HH:mm');    // German (Switzerland)
      locale_map.put('de_DE', 'dd.MM.yyyy HH:mm');    // German (Germany) 
      locale_map.put('de_DE_EURO', 'dd.MM.yyyy HH:mm'); // German (Germany, Euro)
      locale_map.put('de_LU', 'dd.MM.yyyy HH:mm');    // German (Luxembourg)
      locale_map.put('de_LU_EURO', 'dd.MM.yyyy HH:mm'); // German (Luxembourg, Euro)
      locale_map.put('el_GR', 'dd/M/yyyy h:mm a');    //  Greek (Greece)
      locale_map.put('en_AU', 'dd/MM/yyyy hh:mm a');    // English (Australia)
      locale_map.put('en_B', 'MM/d/yyyy h:mm a');
      locale_map.put('en_BM', 'dd/MM/yyyy HH:mm');    // English (Bermuda)
      locale_map.put('en_CA', 'dd/MM/yyyy h:mm a');   // English (Canada) 
      locale_map.put('en_GB', 'dd/MM/yyyy HH:mm');    // English (United Kingdom)
      locale_map.put('en_GH', 'dd/MM/yyyy HH:mm');    //  English (Ghana)
      locale_map.put('en_ID', 'dd/MM/yyyy HH:mm');    //  English (Indonesia)
      locale_map.put('en_IE', 'dd/MM/yyyy HH:mm');    // English (Ireland)
      locale_map.put('en_IE_EURO', 'dd/MM/yyyy HH:mm'); // English (Ireland, Euro)
      locale_map.put('en_IN', 'dd/MM/yyyy hh:mm a');    // English (India)
      locale_map.put('en_NZ', 'dd/MM/yyyy hh:mm a');    //  English (New Zealand)
      locale_map.put('en_SG', 'dd/MM/yyyy HH:mm');    //  English (Singapore)
      locale_map.put('en_US', 'M/dd/yyyy hh:mm a');     // English (United States)
      locale_map.put('en_ZA', 'yyyy/MM/dd hh:mm a');    // English (South Africa)
    locale_map.put('en_PH', 'MM-dd-yyyy hh:mm a');      // English (Philippines)
      locale_map.put('es', 'dd/MM/yyyy HH:mm');     //  Spanish 
      locale_map.put('es_AR', 'dd/MM/yyyy HH:mm');    // Spanish (Argentina)
      locale_map.put('es_BO', 'dd-MM-yyyy hh:mm a');    // Spanish (Bolivia)
      locale_map.put('es_CL', 'dd-MM-yyyy HH:mm');    // Spanish (Chile)  
      locale_map.put('es_CO', 'dd/MM/yyyy hh:mm a');    // Spanish (Colombia)
      locale_map.put('es_CR', 'dd/MM/yyyy hh:mm a');    // Spanish (Costa Rica)
      locale_map.put('es_EC', 'dd/MM/yyyy HH:mm');    // Spanish (Ecuador)
      locale_map.put('es_ES', 'dd/MM/yyyy HH:mm');    // Spanish (Spain)
      locale_map.put('es_ES_EURO', 'dd/MM/yyyy HH:mm'); //  Spanish (Spain, Euro)
      locale_map.put('es_GT', 'dd/MM/yyyy hh:mm a');    // Spanish (Guatemala)
      locale_map.put('es_HN', 'MM-dd-yyyy hh:mm a');    // Spanish (Honduras)
      locale_map.put('es_MX', 'dd/MM/yyyy hh:mm a');    // Spanish (Mexico)
      locale_map.put('es_PE', 'dd/MM/yyyy hh:mm a');    // Spanish (Peru)
      locale_map.put('es_PR', 'MM-dd-yyyy hh:mm a');    // Spanish (Puerto Rico)
      locale_map.put('es_PY', 'dd/MM/yyyy hh:mm a');    // Spanish (Paraguay)
      locale_map.put('es_SV', 'MM-dd-yyyy hh:mm a');    // Spanish (El Salvador)
      locale_map.put('es_UY', 'dd/MM/yyyy hh:mm a');    // Spanish (Uruguay)
      locale_map.put('es_VE', 'dd/MM/yyyy hh:mm a');    // Spanish (Venezuela)
      locale_map.put('et_EE', 'dd.MM.yyyy H:mm');     // Estonian (Estonia)
      locale_map.put('fi', 'd.M.yyyy H:mm');        // Finnish 
      locale_map.put('fi_FI', 'd.M.yyyy H:mm');     // Finnish (Finland)
      locale_map.put('fi_FI_EURO', 'd.M.yyyy H:mm');    // Finnish (Finland, Euro)
      locale_map.put('fr', 'dd/MM/yyyy HH:mm');     // French 
      locale_map.put('fr_BE', 'dd/MM/yyyy H:mm');     // French (Belgium)
      locale_map.put('fr_CA', 'yyyy-MM-dd HH:mm');    // French (Canada)
      locale_map.put('fr_CH', 'dd.MM.yyyy HH:mm');    // French (Switzerland)
      locale_map.put('fr_FR', 'dd/MM/yyyy HH:mm');    // French (France)
      locale_map.put('fr_FR_EURO', 'dd/MM/yyyy HH:mm'); // French (France, Euro)
      locale_map.put('fr_LU', 'dd/MM/yyyy HH:mm');    // French (Luxembourg)
      locale_map.put('fr_MC', 'dd/MM/yyyy HH:mm');    // French (Monaco)
      locale_map.put('hr_HR', 'dd.MM.yyy HH:mm');       // Croatian (Croatia)
      locale_map.put('hu', 'yyyy.MM.dd. H:mm');     // Hungarian 
      locale_map.put('hy_AM', 'M/d/yyyy h:mm a');     // Armenian (Armenia)
      locale_map.put('is_IS', 'd.M.yyyy HH:mm');      // Icelandic (Iceland)
      locale_map.put('it', 'dd/MM/yyyy HH.mm');     // Italian 
      locale_map.put('it_CH', 'dd.MM.yyyy HH:mm');    // Italian (Switzerland)
      locale_map.put('it_IT', 'dd/MM/yyyy H.mm');     // Italian (Italy)
      locale_map.put('iw', 'HH:mm dd/MM/yyyy');     // Hebrew 
      locale_map.put('iw_IL', 'HH:mm dd/MM/yyyy');    // Hebrew (Israel)
      locale_map.put('ja', 'yyyy/MM/dd H:mm');      // Japanese 
      locale_map.put('ja_JP', 'yyyy/MM/dd H:mm');     // Japanese (Japan)
      locale_map.put('kk_KZ', 'M/d/yyyy h:mm a');     // Kazakh (Kazakhstan)
      locale_map.put('km_KH', 'M/d/yyyy h:mm a');     // Khmer (Cambodia)
      locale_map.put('ko', 'yyyy. M. d a h:mm');      // Korean (North Korea)
      locale_map.put('ko_KR', 'yyyy. M. d a h:mm');   // Korean (South Korea)
      locale_map.put('lt_LT', 'yyyy.M.d HH.mm');      // Lithuanian (Lithuania)
      locale_map.put('lv_LV', 'yyyy.d.M HH:mm');      // Latvian (Latvia)
      locale_map.put('ms_MY', 'dd/MM/yyyy h:mm a');   // Malay (Malaysia)
      locale_map.put('nl', 'd-M-yyyy H:mm');        // Dutch 
      locale_map.put('nl_BE', 'd/MM/yyyy H:mm');      // Dutch (Belgium)
      locale_map.put('nl_NL', 'd-M-yyyy H:mm');     // Dutch (Netherlands)
      locale_map.put('nl_SR', 'd-M-yyyy H:mm');     // Dutch (Suriname)
      locale_map.put('no', 'dd.MM.yyyy HH:mm');     // Norwegian 
      locale_map.put('no_NO', 'dd.MM.yyyy HH:mm');    // Norwegian (Norway) 
      locale_map.put('pl', 'yyyy-MM-dd HH:mm');     // Polish 
      locale_map.put('pt', 'dd-MM-yyyy HH:mm');     // Portuguese 
      locale_map.put('pt_AO', 'dd-MM-yyyy H:mm');     // Portuguese (Angola)
      locale_map.put('pt_BR', 'dd/MM/yyyy HH:mm');    // Portuguese (Brazil)
      locale_map.put('pt_PT', 'dd-MM-yyyy H:mm');     // Portuguese (Portugal)
      locale_map.put('ro_RO', 'dd.MM.yyyy HH:mm');    // Romanian (Romania)
      locale_map.put('ru', 'dd.MM.yyyy H:mm');      // Russian
        locale_map.put('ru_KZ', 'dd.MM.yyyy H:mm');       // Russian (Kazakhstan)
        locale_map.put('ru_RU', 'dd.MM.yyyy H:mm');       // Russian (Russia)
      locale_map.put('sk_SK', 'd.M.yyyy H:mm');     // Slovak (Slovakia)
      locale_map.put('sl_SI', 'd.M.y H:mm');        // Slovenian (Slovenia)
      locale_map.put('sv', 'yyyy-MM-dd HH:mm');     // Swedish 
      locale_map.put('sv_SE', 'yyyy-MM-dd HH:mm');    // Swedish (Sweden)
      locale_map.put('th', 'M/d/yyyy h:mm a');       //Thai 
      locale_map.put('th_TH', 'd/M/yyyy, H:mm a');    // Thai (Thailand)
      locale_map.put('tr', 'dd.MM.yyyy HH:mm');     // Turkish
        locale_map.put('tr_TR', 'dd.MM.yyyy HH:mm');      //  Turkish (Turkey)
      locale_map.put('ur_PK', 'M/d/yyyy h:mm a');     // Urdu (Pakistan)
      locale_map.put('vi_VN', 'HH:mm dd/MM/yyyy');    // Vietnamese (Vietnam)
      locale_map.put('zh', 'yyyy-M-d ah:mm');       // Chinese 
      locale_map.put('zh_CN', 'yyyy-M-d ah:mm');      // Chinese (China)
      locale_map.put('zh_HK', 'yyyy-M-d ah:mm');      // Chinese (Hong Kong SAR China)
      locale_map.put('zh_TW', 'yyyy/M/d a h:mm');     // Chinese (Taiwan)
      return locale_map; //return the map
    }

This method shows the datetime in the required timezone and in the userlocale date format. but is there a way to show the converted date in the dateformat corresponding to the timezone to which the datetime is converted.

  • is this in lightning? LWC or AURA? – salesforce-sas Aug 8 '19 at 9:22
  • @salesforce-sas Not in lightning. I am using back bone Js for UI. But these conversions i am handling it in controller itself and just displaying it in backbone Js UI – Sisir Aug 8 '19 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.