In some real-time use cases, we need to convert the date time of one time-zone to the date time of another timezone. This post will show how to convert Datetime of one Timezone to Datetime of another Timezone.
Apex for converting time in a different timezone
This TimeZoneService class will convert datetime of one timezone to another timezone. To convert the timezone, we need the correct TIMEZONESIDKEY. Here is the complete list of supported timezone in salesforce. If we will use an incorrect or not supported timezonesidkey, then we will get GMT timezone information.
Let’s use the above service class to convert time to a different timezone.
1. Convert Datetime to another Timezone
We can convert any datetime value from one timezone to another timezone. Use the above created TimeZoneService method to convert it.
DateTime dtTime=DateTime.now();
DateTime dtTimeTimeZone=new TimeZoneService() .convertDateTimeToOtherTimeZone(dtTime, 'America/Santiago','America/New_York');
2. Convert Time to another Timezone
Normally we save the best time to call with the timezone of the patient, the customer in the salesforce record. If we have saved the best time to call then we can use the below code to convert it into the customer’s timezone. In the below code acct.TimeToCall__c is the account’s object field that needs to be converted. Based on your requirement change the field detail.
Date dt = Date.today();
Time tm=acct.TimeToCall__c;
DateTime dtTime=DateTime.newInstance(dt.year(),dt.month(),dt.day(),tm.hour(),tm.minute(),tm.second());
Time dtTimeZone=new TimeZoneService().convertTimeToOtherTimeZone(dtTime, 'America/Santiago','America/New_York');
Reference:
https://help.salesforce.com/articleView?id=admin_supported_timezone.htm&type=0
13 comments
I can’t see TimeZoneService class code.
Hello Nithya,
It is already there Nithya.
Thank You,
Dhanik
Hi Dhanik, Very good article and very useful as well. but dont know i am not ale to find TimeZoneService in any salesforce help doc. can you please post link to the salesforce doc. unfortunately both examples are not getting executed and giving error “Invalid type: TimeZoneService”
Hello Raju,
TimeZoneService Code exists in the blog. Please click this https://salesforcecodex.com/salesforce/convert-datetime-of-one-timezone-to-datetime-of-another-timezone/ to get this post in non-mobile version. Looks like post directly opening in mobile version so might face that issue.
Thank You,
Dhanik
Why is it just returning Time not DateTime?
Hello Dan,
Please check now, I have added two separate methods for time and date/time conversion.
Thank You,
Dhanik
I have found an issue with this setup that I am not sure if there is a clean solution for. If you were trying to convert a date in Daylight Savings Time when the current time is in Standard Time or vice versa, you will get the wrong time upon conversion because when you get the offset for the target timezone it will be in the current time(either Daylight Savings or Standard depending on season) and your offset will not necessarily be correct for the season of the source datetime.
Hello Java,
As per document, getOffset will take care of daylight saving time if the date argument falls within daylight saving time for time zone. Let me know, in which sceneio it is failing. We can also connect and discuss on this.
Thank You,
Dhanik
Is it possible convert event start date and time to customer timezone which is a picklist value (Hwaii Time, Alaska Time, Pacific Time) that shows customer time?
Yes Sai, It can do this.
Thank You,
Dhanik
Hi Dhanik,
Thank you so much for posting this and it is really helpful. There is one scenario which fails and to overcome this you need to use DateTime.newInstanceGMT() instead of DateTime.newInstance() .
//Convert Any DateTime to other timezone
public static DateTime convertDateTimeToOtherTimeZone(DateTime dttime, string sourceTimezone, string targetTimezone)
{
DateTime targetDT = convertToOtherTimeZone(dttime,sourceTimezone,targetTimezone);
return DateTime.newInstanceGMT(targetDT.year(),targetDT.month(),targetDT.day(),targetDT.hour(), targetDT.minute(), targetDT.second());
}
Thank You Abhishek for sharing code. Please explain in which scenerio code failed so that i will put relavant detail in post.
[…] Check out our other post to Convert Datetime of one Timezone to Datetime of another Timezone. […]