[SalesForce] Salesforce displaying Time custom field in GMT instead of User’s timezone

We are facing an issue with BST and GMT time zone.
We have a community portal and the org timezone and all the Users timezone are set as BST.
Part of our functionality is to book an event and we redirect our users (on the community portal) to a third party system which displays available slots in BST.

Once the user provides their detail and confirms their booking, this third party system then POSTS back into salesforce and create a record of our custom Event booking object.

The Event Booking object has a custom field Booking_Start_Time__c and Booking_End_Time__c.
The third party system, for an event booked for 9:00 AM to 9:30 AM, send

{"Booking_Start_Time__c" : "08:00:00.000Z",
 "Booking_End_Time__c" : "08:30:00.000Z"}

Essentially, they are sending the GMT time (-1 for the actual booking time) for booking start and end times.
The issue is Salesforce displays this as is i.e. "08:00" and "08:30" on the Event Booking detail page even though the User's timezone is set as BST

I am unable to explain this behaviour and would appreciate if someone can shed some light on this.

Adding some screenshots

  1. User time zone : enter image description here
  2. Event booking display page : enter image description here
  3. Same record when accessed in workbench : enter image description here

Best Answer

All DateTimes in Salesforce are stored as GMT/UTC so that they can easily be shifted based on a user's timezone (a necessity in any organization where users are logging in from multiple time zones). So if you see GMT in the database, that's as designed. If the time isn't being shown properly to the end user, consider using a component that displays a DateTime based on a user's locale such as formatted-date-time in a LWC, or lightning:formattedDateTime in an Aura component. You can also render a DateTime in a specific timezone using the DateTime.format() method in Apex or a combination of date(), which returns the current date in the context of the running user, and time() which returns the current time in the context of the running user.

DateTime myDateTime = DateTime.newInstance(1999, 1, 1, 1, 0, 0);
System.debug('My User format (taking into account Locale and TimeZone): ' + myDateTime.format());
System.debug('Perth: ' + myDateTime.format('MM/dd/yyyy hh:mm:ss a z', 'Australia/Perth'));
System.debug('Seoul: ' + myDateTime.format('MM/dd/yyyy hh:mm:ss a z', 'Asia/Seoul'));
System.debug('Los Angeles: ' + myDateTime.format('MM/dd/yyyy hh:mm:ss a z', 'America/Los_Angeles'));

This outputs (I ran this as a user with an American locale and an EDT timezone):

My User format (taking into account Locale and TimeZone): 1/1/1999 1:00 AM
Perth: 01/01/1999 02:00:00 PM AWST
Seoul: 01/01/1999 03:00:00 PM KST
Los Angeles: 12/31/1998 10:00:00 PM PST

EDIT

I think I see the problem and it's my own inexperience with "Time" custom fields (they're relatively new) as opposed to "DateTime" custom fields. Time fields don't react to a user's location. That's because "11:00", "14:00", "12:50 AM" would all be valid inputs to a Time field and - without a date component - it's not possible to know if Daylight Savings Time is in effect or how far to shift a time based on where you are in the world. See some of the notes from Salesforce regarding the Time field or this Trailblazers post. Relevant answer quoted below from Amnon Kruvi:

Since the question is about Time fields, and not DateTime, the answer is no - they are not converted to a timezone. The reason for this is that timezones are date-dependent (thanks to daylight saving time). Hence, without an anchor to the date, it is not possible to convert a time between timezones.

If you know you will only ever need to support BST timezone (and Daylight Savings isn't a factor) and no one in the org will ever be another timezone, it's probably safe to set the timezone for the org to BST and use a Time field. However, if you need to support multiple timezones I would switch to using a DateTime field instead to accurately capture times.

Related Topic