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
KevinShukKevinShuk 

@RemoteAction and Date arguments from javascript

The documentation for javascript remoting says, "Your method can take Apex primitives as arguments," and I'd like to pass a Javascript Date into my remoted function. Javascript Date has a time component, so it's analogous to an Apex DateTime.

 

However, when I pass in the Date from javascript calling my remoted function, the remoting javascript code throws an exception complaining that I'm not passing in a DateTime but rather the time as an ISO string. 

 

I noticed that in the opposite direction, DateTimes get marshaled as an integer of the number of milliseconds since the epoch (Jan 1, 1970), which javascript can use in a Date constructor, so I tried passing that in for the DateTime arg, but it still complained.

 

Is there a way to pass a Javascript Date directly to a remoted function as an Apex DateTime??

 

My workaround in the meantime was to accept the Date as an ISO string, replace the "T" with a space, and use the result to construct the Apex DateTime with DateTime.valueOf. It'd be nice to be able to skip this, though. 

 

Thanks,

--Kevin

Best Answer chosen by Admin (Salesforce Developers) 
cwall_sfdccwall_sfdc

Hey Kevin.

 

Try passing the result of Date.toUTCString() to the client-side remote method.

All Answers

cwall_sfdccwall_sfdc

Hey Kevin.

 

Try passing the result of Date.toUTCString() to the client-side remote method.

This was selected as the best answer
KevinShukKevinShuk

Awesome - this seems to do the trick.

 

Is this in the docs somewhere, and I missed it?

 

Gracias!

Mohan Raj 33Mohan Raj 33
@KevinShuk 
I also want the same result for my requirement Can you please post the sample code for that