How can I format datetime Salesforce?

693    Asked by AashnaSaito in Salesforce , Asked on Sep 22, 2022

Using Apex, how can I format a Datetime variable the same way it is displayed in SOQL results? It seems that SOQL uses the UTC format. When I run the following query

SELECT Created Date FROM Account

I get the following datetime format:

2017-01-10T14:53:55.000+0000


Answered by Abdul Rehman

Using the example given in the Date Time Salesforce class and calling the format GMT(string) method with a formatting string found in the Java Simple Date Format examples:


Date Time my Date Time = Date Time. New Instance(1993, 6, 6, 3, 3, 3); String formatted = my Date Time. format GMT('yyyy-MM-dd'T'HH:mm:ss.SSSZ'); system. Debug (formatted);

Produces this result:

  15:41:47:003 USER_DEBUG [3]|DEBUG|1993-06-06T10:03:03.000+0000


Your Answer

Interviews

Parent Categories