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
Noam.dganiNoam.dgani 

Datetime issue

Hi All

 

My scenario is as follows:

 

I have a REST service that recieves a datetime string with milliseconds.

i need to populate a Datetime field in SF with that value.

 

the problem is that converting a string to datetime ommits the milliseconds.

 

i can manipulate the string to any form that will work - but none seem to.

 

any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

Got the answer in case anybody needs it:

 

lets say that the string you are getting looks like this:

2012-07-07 15:49:26_554

(its usually with period after the seconds - but i got it with underscore)

 

use the following to get the milliseconds into the datetime in your object:

public static DateTime string_to_datetime_milli_precision(String s_date_time)
{
	List<String> split_date = s_date_time.split('_');
        DateTime dt = DateTime.ValueOf(split_date[0]);
        Date d = Date.newInstance(dt.year(),dt.month(),dt.day());
        Time t = Time.newInstance(dt.hour(),dt.minute(),dt.second(),Integer.valueOf(split_date[0]));
        return Datetime.newInstance(d,t);
}