I need to convert and assign 24 hours date time value to custom date time field. For Example, in my apex code I am getting the date time value as ‘2019-03-12 14:38:00’, I need to convert and assign this value to custom date time field as 2019-03-12 02:38 PM in the Salesforce UI. The format methods that I used so far converts '2019-03-12 14:38:00' to '2019-03-12 10:38 AM' which is my user time zone.

I am not looking for time zone conversion, just format the date time.

  • Typically, presenting a date value in the locale (and time zone) of the current user is the responsibility of the platform. It's usually best to store DateTime values in a DateTime field and let the platform do its work, rather than storing formatted string representations, unless there is a specific need for one formatted sort of date.
    – David Reed
    Mar 20, 2019 at 19:16
  • Directly assigned the date time value 2019-03-12 14:38:00 to my custom date time field, but in Salesforce it is showing the value as 2019-03-12 10:38 AM. Salesforce converts the value to my local timezone(GMT-4). Mar 20, 2019 at 19:22
  • Salesforce converts the value to my local timezone -- that's the expected behavior. If you want to view the datetime in GMT, then you will need to change your timezone to GMT. Also showing a datetime field not in User's timezone may cause confusion, what's your use case here?
    – Jayant Das
    Mar 20, 2019 at 19:58
  • @JayantDas - To simplify the requirement. I need to see the value '2019-03-12 14:38:00' as 2019-03-12 02:38 PM in Salesforce. Mar 20, 2019 at 20:03

1 Answer 1


Depending on exactly what you want to achieve, if you just want to output a date/time as text on a page you can use the Datetime.format method that accepts the output format you want to show the value in along with the timezone to use. If you want the value in UTC, use the "GMT" timezone SID.

Remember that Salesforce stores all Datetime values in the database in UTC and automatically presents these values in the current user's timezone unless you do something like the conversion I mention.

The Salesforce documentation provides a link to the Java SimpleDateFormat documentation that explains how to create the output format. Your formatting would be achieved by use of the format string "yyyy-MM-dd hh:mm a".

Note that assigning any value to a Datetime__c field that is a Datetime will always result in a value stored in UTC. 12 and 24 hour presentation is a UI only format.

  • To add to this answer, the Datetime class in Apex also offers a formatGmt method to format a datetime based on a SimpleDateFormat string but keeping it in the GMT timezone. In Visualforce you can use apex:outputText like so: <apex:outputText value="{0,date,yyyy.MM.dd G 'at' HH:mm:ss z}"> <apex:param value="{!myDateTime}" /> </apex:outputText> Aura components have lightning:formattedDateTime and Lightning Web Components have lightning-formatted-date-time for formatting DateTimes.
    – nbrown
    Sep 4, 2019 at 14:03

You must log in to answer this question.

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