Skip to main content
Code blocks Apex -> Java
Source Link
nbrown
  • 4.1k
  • 2
  • 22
  • 46

All datetimesDateTimes 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'));
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'));

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'));

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'));
added notes on Time field
Source Link
nbrown
  • 4.1k
  • 2
  • 22
  • 46

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.

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

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.

Source Link
nbrown
  • 4.1k
  • 2
  • 22
  • 46

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