+ 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).