Why there is a datetime format so different as compare to what I see in the UI vs SOQL/APEX and I can not figure out what is going on. Can anybody please explain or what I'm doing wrong here?

In the UI this is what I see:

5/27/2021 6:30 AM

and when I query I see this:


when I ran the apex code I see this:

2021-05-27 11:30:00

String data = '2021-05-27T11:30:00.000+0000';
Datetime dt = (DateTime)JSON.deserialize('"' + data + '"', DateTime.class);
system.debug(  dt);

My question, why is it not showing what is in the UI?


The UI format is just a more user-friendly format, shown in your preferred timezone, converted from GMT on the back end.

The results of your query are the IS0-8601 format, how it is stored on the back end.

The output of your Apex code is the default formatting of the Datetime primative data type.

Additional Resource on Datetime methods on Apex: https://developer.salesforce.com/docs/atlas.en-us.232.0.apexref.meta/apexref/apex_methods_system_datetime.htm

  • how would you convert the 2021-05-27T11:30:00.000+0000 to ui format? the example I see here (developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/…) does not really work since I have zeros and +
    – Nick Kahn
    May 27 at 14:40
  • @NickKahn - I'm not sure why you'd want to do that when most custom development in Salesforce (e.g. LWC) will format the date for you on the front end and you provide the datetime in a predesignated data type or format like ISO-8601. If you really need to, then you could do something like this: String strConvertedDate = dt.format('MM/dd/yyyy HH:mm:ss a');
    – Coogle
    May 27 at 16:25
  • okay understood but why is the time difference in the ui it says 6:30 and backend it says 11:30
    – Nick Kahn
    May 27 at 17:10
  • 1
    timezones. Your user in Salesforce is set with a timezone that has an offset of UTC -5:00 so in the UI, the time is adjusted by 5 hours. Apex and the database do not adjust for user's timezone offsets.
    – Mark Pond
    May 27 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.