[SalesForce] Salesforce datetime field adding user timezone to the field

I have two datetime field in custom object, to which I am inserting values programatically from my apex class.

Fields from my custom object
enter image description here

My apex code used to insert/update records in the custom object

public Datetime appointmentFrom{get;set;}
public Datetime appointmentTo{get;set;}
public String opportunityId{get;set;}
public String recordName{get;set;}
 
customObject__c custom = new customObject__c();
custom.Name =recordName;
custom.Opportunity__c = opportunityId;
custom.AppointmentFrom__c =appointmentFrom;
custom.AppointmentTo__c=appointmentTo;
insert custom;

Value I have selected is

enter image description here

The output I received[Value from inserted record]

enter image description here

A timezone of GMT+5:30 has been added to my selected datetime and is saved in the record. I don't want any timezone to be added with my record. I just want my selected value to get saved. How to do so?

Best Answer

DateTime values are always stored in the database as GMT. They are always converted to the user's local time zone when being displayed. You need to convert the DateTime values in to GMT and go from there. In many cases, this is done for you automatically (e.g. Visualforce), but if you're trying to calculate this yourself, you must use GMT-based calculations. There's no such thing as a "date time field in a fixed time zone" short of using a normal text field and storing the value as text, and even then, you're going to have to do the time conversions manually.

Related Topic