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