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
ptepperptepper 

RemoteAction returns long number for date in Javascript Result - how to I convert to readable date?

 

I'm returning a query result as an SObject from a RemoteAction method in Apex that includes an ActivityDate field on it, which should be type date. When I get it in the Javascript it's a long number like 1312848000000. 

 

I'm guessing this is some internal storage format, but I don't think it's a normal Unix timestamp. Can someone tell me how to convert this to a readable date string, either in the Javascript or the Apex?

 

thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ptepperptepper

I guess no one knew the answer to this. I found it buried in another thread:

 

http://boards.developerforce.com/t5/Visualforce-Development/RemoteAction-and-Date-arguments-from-javascript/td-p/311691

 

It's the Unix time * 1000 (milliseconds since the epoch). Javascript takes this as an argument to the constructor of it's Date object (var d = new Date(milliseconds).

 

 Note, the time returned is in UTC time so you need to use the UTC versions of the Date object functions in javascript (getUTCMonth,getUTCDate, etc.). I got stuck on that one for a bit too.

All Answers

ptepperptepper

I guess no one knew the answer to this. I found it buried in another thread:

 

http://boards.developerforce.com/t5/Visualforce-Development/RemoteAction-and-Date-arguments-from-javascript/td-p/311691

 

It's the Unix time * 1000 (milliseconds since the epoch). Javascript takes this as an argument to the constructor of it's Date object (var d = new Date(milliseconds).

 

 Note, the time returned is in UTC time so you need to use the UTC versions of the Date object functions in javascript (getUTCMonth,getUTCDate, etc.). I got stuck on that one for a bit too.

This was selected as the best answer
Manan Shah 15Manan Shah 15

Hi ptepper,

I know it's very long duration since this thread was open. But recently I encountered similar situation where I need to get answer of following use case:

I am using angular JS in VF page which calls remote action apex method on click of button.
Apex method returns response in terms of apex wrapper class where some of the parameters are of "Date" type

The situation is when this apex response is assigned to JS variable in angular script, those "Date" type variables are converted into milliseconds. WHY THIS IS HAPPENING THAT I AM NOT SURE? if someone knows about it then please tell here.

The impact of this ms value is VF page displays that JS variable in UI according to system's time zone which may vary from one user to another if both are in different time zone because that ms value considers date + time value.

In Salesforce, it is only Date component. So regardless of salesforce user's time zone, it will display same value in SFDC detail page but this is not the case in UI which is using JS to store date value