0 votes
1 view
in Salesforce by (11.9k points)

I was wondering if there is an apex date or datetime function to format a date/time to the ISO formatted timestamp "2007-04-05T12:30-02:00" or would this have to be created by another means?

Thanks.

1 Answer

0 votes
by (31.8k points)

DateTime format (string dataFormat) or format(string dataFormat, string timezone) methods can be used in Apex. It accepts a dataFormat string, which corresponds to a Java simple date format. You will need to define the correct format for ISO 8601.

Do take into account the timezone of the DateTime. In the below example, I have used the GMT format to avoid the timezone offset.

System.debug(datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''));

Alternatively, you can use the JSON serializer.

System.debug(json.serialize(datetime.now()));

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 5, 2020 in Salesforce by dante07 (10.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...