Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Convert Datetime of one Timezone to Datetime of another Timezone

    Convert Datetime of one Timezone to Datetime of another Timezone

    Dhanik Lal SahniBy Dhanik Lal SahniApril 9, 2021Updated:June 11, 202313 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Convert Datetime of one Timezone to Datetime of another Timezone
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_timezone.htm

    apex lightning salesforce timezone timezone conversion timezonesidkey
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleZip Code to Time Zone using ZipCodeAPI in Salesforce
    Next Article Monitor Salesforce API Limit
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    View 13 Comments

    13 Comments

    1. Nithya on June 19, 2021 7:25 pm

      I can’t see TimeZoneService class code.

      Reply
      • Dhanik Lal Sahni on June 27, 2021 7:13 pm

        Hello Nithya,
        It is already there Nithya.

        Thank You,
        Dhanik

        Reply
    2. Raju Kumar on July 7, 2022 3:04 pm

      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”

      Reply
      • Dhanik Lal Sahni on July 9, 2022 9:12 am

        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

        Reply
    3. Dan on February 15, 2023 8:31 pm

      Why is it just returning Time not DateTime?

      Reply
      • Dhanik Lal Sahni on February 26, 2023 8:34 pm

        Hello Dan,

        Please check now, I have added two separate methods for time and date/time conversion.

        Thank You,
        Dhanik

        Reply
    4. Javan Soliday on July 18, 2023 3:40 am

      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.

      Reply
      • Dhanik Lal Sahni on July 18, 2023 6:04 am

        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

        Reply
    5. sai shanmukh on October 12, 2023 3:56 pm

      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?

      Reply
      • Dhanik Lal Sahni on October 13, 2023 7:15 pm

        Yes Sai, It can do this.

        Thank You,
        Dhanik

        Reply
    6. Abhishek Deshpande on December 12, 2023 1:19 pm

      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());
      }

      Reply
      • Dhanik Lal Sahni on December 13, 2023 12:16 pm

        Thank You Abhishek for sharing code. Please explain in which scenerio code failed so that i will put relavant detail in post.

        Reply
    7. Pingback: Get User Time Zone Offset in Salesforce - Salesforce Codex

    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.