Friday, 20 March 2015

How to Convert UTC Time Zone to User Time Zone in Salesforce

SOQL queries in a client application return dateTime field values as Coordinated Universal Time (UTC) values. You need to manually handle to convert this UTC time zone to your User Time zone.

For this salesforce provides, convertTimezone method. But You can use convertTimezone() in a date function.


Let see how to convert UTC time zone to User Time Zone,

  1. DateTime scheduleTime = DateTime.valueofGmt( ‘Your Date Time to be Convert’);
  2. // Get User time zone.              
  3. Timezone userTimezone = UserInfo.getTimezone();
  4. Integer userOffset = userTimezone.getOffset(scheduleTime);
  5. // Get Organization time zone.
  6. Timezone orgTimeZone = Timezone.getTimezone('America/Los_Angeles');
  7. Integer orgOffset = orgTimezone.getoffset(scheduleTime);
  8. Integer offsetDifference = orgOffset - userOffset;
  9. // DateTime is converted from UTC timezone to User Timezone
  10. DateTime correctedDateTime = scheduleTime.addMinutes(offsetDifference / (1000 * 60));

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...