1

Can anyone tell me how to convert the date format to string format in JavaScript Remoting?
Below is my code

@RemoteAction      
    global static theHRMS__Employee__c getEmployee() {    
Contact c=[select birthdate__c from contact where id=0039000001CoN3m];    
return c;   
}  

My VFP:

Visualforce.remoting.Manager.invokeAction(     
                            '{!$RemoteAction.NewUIEmployeeContr.getEmployee}',    

                            function(result, event){    
                                if (event.status) {   
                                     Var bdate=result.birthdate__c ;     
                                      alert(bdate);    
                                     }     

The above code gives alert message like

731808000000

But my actual birthdate is 22/11/1992

I dono how to convert the date to string ?

2

JavaScript not only doesn't handle apex dates well (they get adjusted to locale) because it only understands datetime, but its native datetime manipulation functions are pretty weak.

I throw momentjs (open library) into any org that uses remoting with dates...saves all kind of complexity.

Moment will format all your dates any way you want them, compare them properly (js won't with some ugly hacks!!) and do things like time zone adjustments to undo what Salesforce does via locale.

1

Try this : http://blogforce9.blogspot.fr/2013/02/handling-dates-in-visualforce-js.html

I did not test it, but it sounds a good solution. I faced the same problem before, what I did is to return String in my remote action and not date.

0

To convert a JavaScript date object into a string, simply omit the "new" keyword when instantiating the date object.

Use this: Date();. - returns typeof "string".

Do not use this: new Date();. - returns typeof "object".

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.