2

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];
}
3

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;
}
1
  • This worked: var date = new Date(results.Date__c); results.Date__c = date.toLocaleDateString();
    – user569925
    Nov 17 '15 at 15:27
0

I tried in a different way to show up for the format "MM/DD/YYYY HH:MM AM/PM"

Following is the code snippet:

    function formatDate(dateInMicroSeconds)
    {
        var vDate = (new Date(dateInMicroSeconds)).toLocaleString().replace(', ',' ');
        vDate  = vDate.slice(0, vDate.indexOf(':') + 3) + vDate.slice(vDate .indexOf(':') + 6);
        window.console.log('vDate' + vDate);
        return vDate;
    }
    formatDate(1482238284000);

Hope it helps too!

for more details refer blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.