Find your content:

Search form

You are here

Workarounds for Missing Apex Time.format() Instance Method


Calling results in '28/08/2012 16:20' for current user locale 'French (France)' & '8/28/2012 4:20 PM' for current user locale 'English (United States)'.

Calling results in '28/08/2012' for current user locale 'French (France)' & 8/28/2012 for current user locale 'English (United States)'.

Ideally a Time.format() instance method would exist so that results in '16:20' for current user locale 'French (France)' & '4:20 PM' for current user locale 'English (United States)'.

Please suggest any and all locale sensitive workarounds for formatting the Time part only.

In my particular case, I'm not displaying this Time part only to the user via Visualforce, so an Apex workaround is what I'm looking for.

Attribution to: mjgallag

Possible Suggestion/Solution #1

This is a great question.

I can only come up with a workaround of using some external logic to choose between a format string of "HH:mm" for 24-hour mode and "h:mm a" for 12-hour mode.

One possibility would be to create a known datetime with a time in the PM hours (16:56) and a date with different numbers (11/12/2017). Format that to a string. Then try to match various patterns in that string, and use the matches to create the format string for the actual time you want to format:

  • "16:56" -> "HH:mm"
  • "4:56 PM" -> "h:mm a"
  • "4:56PM" -> "h:mma"
  • "PM4:56" -> "ah:mm"
  • "PM 4:56" -> "a h:mm"

Have a default format if you can't make a match.

Once you know the format (should only need to run once per instance), you can take a Time, promote it to a DateTime with some arbitrary date, and use the format method with your calculated format string.

Huge hack, and something that Salesforce should probably address by providing an API to get locale-specific format strings for Date, Time and DateTime.

Note that solutions that try to split the string on spaces won't work for all of the Salesforce supported locales. Some examples:

  • Hebrew, Vietnamese: 16:30 02/01/2008
  • Korean: 2008. 1. 2 PM 4:30 (interestingly, time is shown as 오전 6:00)

Attribution to: tomlogic

Possible Suggestion/Solution #2

You could just split the DateTime format() result on the first space - does that give you what you're looking for?

public String myDateFormat(DateTime dt) {
    String[] parts = dt.format().split(' ');
    return (parts.size() == 3) ? (parts[1] + ' ' + parts[2]) : parts[1]; 


6:38 PM

for me in English (United States), and


in French(France).


As tomlogic points out, the above method is not very robust - some locales may include spaces in the date or time portion of the format, and the ordering is not consistent. This second attempt assumes that the date and time are separated by zero or more spaces, but handles spaces within the two portions, and either ordering of date and time. The only assumption made is that the formatted Date is contained within the formatted Time:

public String myDateFormat(DateTime dt) {
    return dt.format().replace(, '').trim();

Seems to work fine for Hebrew, Vietnamese & Korean, as well as English and French.

Attribution to: metadaddy

Possible Suggestion/Solution #3

String dt =;
Integer s = dt.indexOf(' ');

This should do it. No matter what timezone you are in. I tried it for a few and it worked fine!

Please try and let me know.

Attribution to: subodhbahl
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content