Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Wednesday, July 30
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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 Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 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
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (149) salesforce apex (52) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • 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
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (149) salesforce apex (52) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) 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

    banner
    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.