function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cris9931Cris9931 

How to convert from GMT(Date/Time) to UTC(Date/Time)(no timezone conversion)

Hi, I've wrote a trigger to fetch the old Date/Time from a field and store in a new field.. The problem is that it is converting in Local Time and not in UTC... How can I convert my date in UTC timezone?

trigger StartDateTime on SVMXC__SVMX_Event__c (before update) {
 
 for(SVMXC__SVMX_Event__c even : Trigger.New){
    if(even.SVMX_PS_Validated__c == FALSE && (Trigger.newMap.get(even.id).SVMXC__StartDateTime__c) != Trigger.oldMap.get(even.id).SVMXC__StartDateTime__c) {
       even .Original_Date_StartDateTime__c = Trigger.oldMap.get(even.id).SVMXC__StartDateTime__c;

    }
 }
 

}

I see two different timezones

User 1 : GMT 3

User-added image

User 2 : GMT 2

User-added image

AnudeepAnudeep (Salesforce Developers) 
Hi Cristian - You can make use of the Timezone class to do the conversion. However, it is important to understand the root cause of this behavior

Salesforce stores all Date/Time fields internally in UTC (Universal Time Coordinated) time. If you are not seeing a UTC value, I recommend checking the users time zone

Please see the following resources to learn more about how Date/Time fields are stored

https://help.salesforce.com/articleView?id=000335706&type=1&mode=1

https://help.salesforce.com/articleView?id=formula_using_date_datetime.htm&type=5

https://medium.com/salesforce-zolo/what-every-salesforce-developer-should-know-about-dates-and-times-in-apex-d49bc0a116d4

Let me know if this helps

Thanks, 
Anudeep