0

We have a requirement in which we copy Event.StartDateTime in another field called Activity.Start_Date_Time_Text__c. The calculation of DateTime (UTC at rest) Text happens in before insert and update triggers. I do the conversion using following code snippet.

anEvent.Start_Date_Time_Text__c = dtEventStartDateTime.format('MM/dd/yyyy hh:mm:ss a', 'America/Chicago');

"America/Chicago" here is for testing purposes, it could be any timezone in IANA format. My requirement is to put even the timezone in the text field.

Right now, the text field looks like "11/13/2018 12:30:00 PM" instead it should look like "11/13/2018 12:30:00 PM CST" or "11/13/2018 12:30:00 PM CDT" or "11/13/2018 12:30:00 PM CT"

In the code I only have timezone in the IANA format (i.e. "America/Chicago or "America/Los_Angeles". One of the solutions is for me to maintain a mapping but I was wondering if there is a out of the box solution by which I can do this programmatically instead maintaining a map.

3

You can use a z in your format string to get the abbreviated time zone included in your output. For example,

System.debug(Datetime.now().format('MM/dd/yyyy hh:mm:ss a z', 'America/Chicago'));

yields

15:48:25:002 USER_DEBUG [1]|DEBUG|11/13/2018 02:48:25 PM CST

which is the time of this writing in Chicago (my local time is EST) + the associated time zone.

The Java SimpleDateFormat documentation is the place to go for all the details.

  • Asked and answered, mate! Thanks. I made a mistake of not going to the root and being stuck only in SF documentation. This helps in two ways for me. – Json Bourne Shell Nov 13 '18 at 20:51

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.