Convert DateTime of one timezone to datetime of another Timezone

Convert DateTime of one timezone to datetime of another Timezone

Time zones in Salesforce:-

Time zones come in two varieties — region-based and offset-based. In Salesforce, we see region-based time zones very often. 

If you go to your Company Information page on setup, you would see a list of time zones. For example America/Chicago and America/Los_Angeles. The list of region-based time zones available in Apex is given in an article. Here is the complete list of supported timezones in salesforce. Apex supports all time zones returned by the TimeZone.getAvailableIDs method of the TimeZone class.

 

TimeZone Class:-

Represents a time zone. Contains methods for creating a new time zone and obtaining time zone properties, such as the time zone ID, offset, and display name

You can use the methods in this class to get properties of a time zone, such as the properties of the time zone returned by UserInfo.getTimeZone, or the time zone returned by getTimeZone of this class.

 

Example :-

TimeZone tz = UserInfo.getTimeZone();
System.debug('Display name: ' + tz.getDisplayName());
System.debug('ID: ' + tz.getID());
// During daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,10,23,12,0,0)));
// Not during daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,11,23,12,0,0)));
System.debug('String format: ' + tz.toString());
 

Datetime class:-

Datetime means a value that indicates a particular day and time, such as a timestamp. You can add or subtract an Integer or Double value from a Datetime value, returning a Date value. Addition and subtraction of Integer and Double values are the only arithmetic functions that work with Datetime values. You can’t perform arithmetic functions that include two or more Datetime values. Instead, use the Datetime methods.

 

For example :-

datetime myDateTime = datetime.now();</p>
DateTime dt = DateTime.newInstance(2012, 1, 26, 5, 2, 4);
System.assertEquals(2012, dt.year());
 

So let’s use below code to convert time in different timezone

public class ConvertTimeZoneService{
    
    public static string EST='America/New_York';
    ///Convert Any time to other timezone
    public Time convertDateTimeToOtherTimeZone(DateTime dttime, string sourceTimezone, string targetTimezone)
    {
        TimeZone tz = Timezone.getTimeZone(sourceTimezone);
        System.debug('Display name: ' + tz.getDisplayName());
        System.debug('ID: ' + tz.getID());
        
        Integer offset=tz.getOffset(dttime);
        System.debug('Offset: ' + offset);
        
        // target Timezone.
        TimeZone targetTZ = Timezone.getTimeZone(targetTimezone);
        System.debug('Display name: ' + targetTZ.getDisplayName());
        integer offsetTotargetTZ = targetTZ.getOffset(dttime);
       	integer correction = offsetTotargetTZ- offset;
    
        DateTime targetDT = dttime.addMinutes(correction / (1000 * 60));
        
        return Time.newInstance(targetDT.hour(), targetDT.minute(), targetDT.second(), targetDT.millisecond());
    }
}
 

1. Convert Datetime to another Timezone:-

So to convert DateTime to another timezone we can use the above ConvertTimeZoneService class method.

DateTime dtTime = DateTime.now();
DateTime dtTimeTimeZone = new ConvertTimeZoneService().convertDateTimeToOtherTimeZone(dtTime, 'America/Santiago','America/New_York');
 

So by using the convertDateTimeToOtherTimeZone method we can convert any DateTime to another timezone.

 

2. Convert Time to another Timezone

To convert Time to another TimeZone we can use the ConvertTimeZoneService class method by passing the DateTime value to that method which we want to convert.

Date dt = Date.today();
Time tm = acct.TimeToCall__c; //Here we can use any field which contains time
DateTime dtTime = DateTime.newInstance(dt.year(),dt.month(),dt.day(),tm.hour(),tm.minute(),tm.second());
DateTime dtTimeTimeZone = new ConvertTimeZoneService().convertDateTimeToOtherTimeZone(dtTime, 'America/Santiago','America/New_York');