Im trying to format this String into DateTime.




 String data = '2019-01-03T00:26:01.711-02:00';
 Datetime dt = DateTime.parse(data);


System.TypeException: Invalid date/time: 2019-01-03T00:26:01.711-02:00

  • Salesforce datetime format is 'yyyy-MM-dd HH:mm:ss. Are you trying to achieve timezone? – Pranay Jaiswal Jan 18 '19 at 19:19
  • Just to add to the confusion a bit, there is at least one place where you would use an ISO 8601 datetime. It's used in SOQL queries like so [SELECT Id FROM Account WHERE CreatedDate > 2019-01-03T00:26:01.711-02:00] (yes, without quotes surrounding the datetime in this case). – Derek F Jan 18 '19 at 19:38

To parse DateTimes in this ISO format, you need to use JSON deserialization.

String data = '2019-01-03T00:26:01.711-02:00';
Datetime dt = (DateTime)JSON.deserialize('"' + data + '"', DateTime.class);

The JSON parser understands ISO format, while DateTime.parse() uses the running user's locale settings.


String should be in the local time zone and in the format of the user locale.



Datetime dt = DateTime.parse('10/14/2011 11:46 AM');
String myDtString = dt.format();
system.assertEquals(myDtString, '10/14/2011 11:46 AM');

enter image description here

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.