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;
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?

  • What you see on a record's detail page is based on the timezone of the current logged in User who's viewing the record. So if the logged in User is in GMT, they will see as what you have in the screenshot. How are you capturing the From/To, is it from standard page layout or using any custom component and how isyour appointmentFrom and appointmentTo populated?
    – Jayant Das
    Commented Nov 15, 2018 at 16:38
  • I have From/To in a custom component
    – Aruna
    Commented Nov 15, 2018 at 16:44

1 Answer 1


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.

  • I logged in as my community user to perform this operation and the timezone of my logged in user is (GMT+01:00) Central European Time (Europe/Paris). Why is it taking my System Admin user timezone?
    – Aruna
    Commented Nov 15, 2018 at 16:48
  • @Aruna Are you sure you're not misinterpreting? It should always be the same logic no matter the user. You might need to go in to more detail.
    – sfdcfox
    Commented Nov 15, 2018 at 16:51
  • previously I was having <lightning:input type="datetime"> field in which when I was mapping a value it was taking the logged in community partner user timezone(GMT+01:00) and was adding that value to the datetime field. So I removed the datetime field and added separate <lightning:input type="date"> and <lightning:input type="time"> fields to fetch the input and then added them together to get the datetime value so as to get rid of timezone issue but then again when I inserted my record from apex class it is adding my System Admin user timezone [GMT+5:30] to my record.
    – Aruna
    Commented Nov 15, 2018 at 16:57
  • 2
    @Aruna No, the time is in GMT at that point; you're viewing the output, which is shifted for your time zone. Try viewing the raw data from a query.
    – sfdcfox
    Commented Nov 15, 2018 at 17:02
  • Yes I queried my custom object record and I it is having the value I saved. My code was totally perfect it is just me who was not able to figure out the output Thank you for saving my day!!
    – Aruna
    Commented Nov 15, 2018 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .