TransWikia.com

How to format date field to ISO 8601 format?

Salesforce Asked on November 13, 2021

I need to have a Date field send in an apex API callout in the format that matches this format "2019-05-28T19:40:36+00:00"

I currently have the following:

Datetime startDate = datetime.newInstance(intCan.Training_Class_Start_Date__c.year(),intCan.Training_Class_Start_Date__c.month(),intCan.Training_Class_Start_Date__c.day());
String startDate2 = startDate.formatGmt('yyyy-MM-dd'T'HH:mm:ss:SSSZ');
System.debug('StartDate: '+startDate2);

However this produces StartDate: 2020-07-27T04:00:00:000+0000

One Answer

Since you format to GMT you know that the timezone offset will always be 00:00. Hence you can simply do:

Datetime startDate = Datetime.newInstanceGmt(2020, 7, 21, 20, 40, 00);
String startDate2 = startDate.formatGmt('yyyy-MM-dd'T'HH:mm:ss') + '+00:00';
System.debug('StartDate: ' + startDate2);

(That I create a Datetime instance with newInstanceGmt was just for my benefit; construct it how you need.)

Answered by Phil W on November 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP