Master of Time – Salesforce Apex

Date/Time is a fascinating topic, especially when we are working in multiple timezones environments. We are going to discuss some scenarios which can help you to understand and overcome the numerous timezone issues in Apex.  

First thing you have to know that, all Date/Time values are stored at GMT timezone in Salesforce. The system converts the display value base on the locale you set in the setting.  For example, a user is in GMT+7 timezone (Asia/Saigon) has set a date/time value 5/29/2020 8 AM, the stored value when querying using Apex/SOQL should be display at “2020-05-29 01:00:00” GMT. 

Scenario 1: All parties are in the same timezone.

With this scenario, we actually do not care much about how the value is stored.  We can use the Datetime methods to deal with users timezone. 

Datetime appointmentAt8AM = Datetime.newInstance(Date.newInstance(2020, 05, 25), Time.newInstance(8, 0, 0, 0));

//Print it out 
System.debug(appointmentAt8AM.format('MM/dd/yyyy h:mm a','Asia/Saigon'));

17:35:05:002 USER_DEBUG [2]|DEBUG|05/25/2020 8:00 AM

Scenario 2: All parties are in different timezones. So let’s say, User 1 is in Asia/Saigon GMT+7  timezone; User 2 is in Asia/Singapore GMT+8 timezone.  We can clearly see the difference of 1 hour between 2 users.

The user in Asia/Saigon wants to meet the user in Asia/Singapore. With this, we also treat it as scenario 1.

Datetime appointmentAt8AM = Datetime.newInstance(Date.newInstance(2020, 05, 25), Time.newInstance(8, 0, 0, 0));

//Print it out 
System.debug(appointmentAt8AM.format('MM/dd/yyyy h:mm a','Asia/Saigon'));

17:35:05:002 USER_DEBUG [2]|DEBUG|05/25/2020 8:00 AM

How about when a user what to schedule at another user timezone. With the example, the user can easily calculate the correct time to add/subtract 1 hour. However, a user schedule hundreds of appointments with multiple users in multiple timezones?  

The answer is we need a custom code to convert date/time to correct timezone, and we only care about one timezone the GMT. We will create exact Date/Time in GMT, then subtract the offset; we will have the final result as target date/time.

Example: We want to create a Date/Time in Asia/Singapore timezone, and we do not care about the current user timezone.

Datetime appointmentAt9AMFMT = Datetime.newInstanceGmt(Date.newInstance(2020, 05, 25), Time.newInstance(9, 0, 0, 0));

//Get the Timezone Class of the giving Timezone
Timezone targetTz = Timezone.getTimezone('Asia/Singapore'); 

//get offset of the timezone 
Integer offset = targetTz.getoffset(appointmentAt9AMFMT)/1000; // convert milliseconds to seconds..You can / 60000 to get minute  here.

DateTime targetDatetime = appointmentAt9AMFMT.addSeconds(-offSet);  //Why minus value here? Because 9 AM GMT +8 Should be 1 AM GMT Right? 

System.debug(targetDatetime.format('MM/dd/yyyy h:mm a','Asia/Singapore'));

19:24:35:005 USER_DEBUG [11]|DEBUG|05/25/2020 9:00 AM 

You can see the Date/Time was saved in Asia/Singapore correctly.  

Hope this helps you clear about Date/Time and Timezone. Happy Coding.