[SalesForce] Date formatting when using JavaScript Remoting

I have a JavaScript remoting method that gets a List of custom objects with date. In my JavaScript method, I loop through the above list of objects and print them in a table. For Date__c though I get values such as: 1427068800000, 1427241600000, 1426550400000 & so on.

Is there a way to format all dates in the above list in MM/dd/yyyy format? Do I need to handle this all objects in a list in one go or do I need to iterate through each one? Do I format above date in controller or JavaScript?

Here is my JavaScript:

<script type="text/javascript">
    j$ = jQuery.noConflict();
    j$(document).ready(function() {
        j$('#divTable').append('<table />');
        getRemoteDeals('something');
    });
    function getRemoteData(controllerParams) {
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.MyController.GetData}',
            controllerParams,
            function(result, event){
            if (event.status) {
                    j$('#divTable table').append('<tr> <td>' + results.Date__c +  '</td> </tr>');
                } else if (event.type === 'exception') {
                    document.getElementById("responseErrors").innerHTML = 
                        event.message + "<br/>\n<pre>" + event.where + "</pre>";
                } else {
                    document.getElementById("responseErrors").innerHTML = event.message;
                }
            }, 
            {escape: true}
        );
    }
</script>

Below us my controller method:

@RemoteAction
global static List<Deal__c> GetData(string someParam)
{
    return [Select Id, Name, Date__c from MyCustObj__c where myField__c = :someParam];
}

Best Answer

You could either use some of the Javascript date format functions (i.e.)

var date = new Date(1324339200000);

date.toString("MMM dd"); // "Dec 20"

OR

Change the return type of your remote action:

@RemoteAction
global static String[] GetData(string someParam)
{
    String[] results = New String[]{};
    for(MyCustObj__c o : [Select Id, Name, Date__c from MyCustObj__c where myField__c = :someParam]){
        if(o.Date__c != null) results.add(o.Date__c.format());
    }

    return results;
}