+ Start a Discussion
Jack D PondJack D Pond 

Convert internet date/time String to DateTime

Webhooks and Web services often use ietf Internet Date/Time strings (IETF Date and Time on the Internet: Timestamps: Examples (https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#date.and.time.format.examples))

Most commonly, the format '2021-03-25T12:26:00.00Z'

Datetime xPassedTime = Datetime.valueOf('2021-03-25T12:26:00.00Z');
will return an error of "System.TypeException: Invalid date/time: 2021-03-25T12:26:00.0000Z"
However, if you change the 'T' to a space, it works fine.  Example:
Datetime xPassedTime = Datetime.valueOf('2021-03-25T12:26:00.00Z'.replace('T',' '));
Bizarrely, the JSONParser getDatetimeValue() works just fine with the standard format.
Q1: Am I going about this correctly?
Q2: Is this an enhancement request or bug?
Q3: Is the .replace('T',' ') a best-practice workaround (and if so, mark this as answer).


I recently ran into this exact same issue and decided to use

JSON.deserialize('"' + '2021-03-25T12:26:00.00Z' + '"', DateTime.class)

(see https://github.com/tcampb/calendly-salesforce-sdk/blob/main/force-app/main/default/classes/CalendlyApi.cls#L189-L192)

I'm not 100% sure if this is considered a best practice but I did find this method commonly referenced in stackexchange / stackoverflow posts while I was researching this.