function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
__ 

DateTime.parse () help

I have a date time string from external system that is represented as 10/13/2011 10:36:07 AM

Need to put it in datetime field on sf object. when try to do datetime.parse ('10/13/2011 10:36:07 AM'); it gives me type exception, invalid date time... any workarounds?

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz

If you remove the seconds from the time and use

 

datetime.parse ('10/13/2011 10:36 AM');

 

instead, it works. 

 

I also noticed that our documentation for datetime.parse is pretty lacking.  That will be corrected soon.

 

Thanks for pointing this out.

 

 

All Answers

spraetzspraetz

If you remove the seconds from the time and use

 

datetime.parse ('10/13/2011 10:36 AM');

 

instead, it works. 

 

I also noticed that our documentation for datetime.parse is pretty lacking.  That will be corrected soon.

 

Thanks for pointing this out.

 

 

This was selected as the best answer
__
i need to parse string like it is. i know it works without seconds... but in my case string comes in that format from webcallout response and i need a way to get it in sf datetime field.

on a side note- why is the person who replied marks answer as solved? shouldnt that be up to me to decide?
thanks.
spraetzspraetz

The workaround would be to parse the string and strip out the seconds.

Adam TolbertAdam Tolbert
This appears to be a continuing issue... and Im not really sure why this hasnt been resolved. It seems like such a minor change that would impact almost everyorg, it wouldnt be a breaking change (if not passed seconds, it would still intuit the seconds value as 00) and while I am currently able to workaround it by splititng the string into an array on ':', removing the first two characters of the index[2], rejoining the string array into one string, and then reinserting that new mangled string into the formula, this is in my opinion, something that should work out of the box. I should not have to do manual string manipulation just to pass a datetime from one database to another. How this has slipped through the ~24 updates to SF since then is a mystery to me but hey, were only paying for the software. 
Kevin BrussKevin Bruss

It looks like as of api v45 salesforce added feature of a comma after the date string.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_datetime.htm#apex_System_Datetime_parse
 

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