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
SS KarthickSS Karthick 

Date to String in JavaScript Remoting

Hi folks,
        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 ?




Thanks in advance
Karthick
Best Answer chosen by SS Karthick
Amritesh SahuAmritesh Sahu
Hi Karthick,

Use following code
var d = new Date(result.birthdate__c);
var strdate = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear();
alert(strdate);

If the input is 731808000000 , then the result in the popup you will get  "11/3/1993";(In dd/mm/yyyy format).

Regards,
Amritesh

All Answers

Amritesh SahuAmritesh Sahu
Hi Karthick,

Use following code
var d = new Date(result.birthdate__c);
var strdate = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear();
alert(strdate);

If the input is 731808000000 , then the result in the popup you will get  "11/3/1993";(In dd/mm/yyyy format).

Regards,
Amritesh
This was selected as the best answer
SS KarthickSS Karthick
@Amritesh Sahu,
         Thanks for your response.
      Can you tell me how it works?
Whats the purpose of d.getmonth()+1 ...

Thanks in advance
Karthick
Amritesh SahuAmritesh Sahu
Hi Karthick,

The number '731808000000' is in milliseconds. (JavaScript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time).
So we created 'd' date type using the number.
Now the actual date is stored in d, we fetched the date using d.getDate() and so on.
d.getMonth() returns value inrange of 0-11, so to get actual month I had to increment it.

To know about javascript Date methods, follow link 
http://www.w3schools.com/jsref/jsref_obj_date.asp

Thanks,
Amritesh