public class MyCustomController1
{
public class studObjectwrap
{public String D_ob{get;set;}}
@RemoteAction
public static String createRecord(String jsonObjResult){
//Some Code od Json Deserialization
Student__c st=new Student__c();
//Converting Date Into Salesforce Date Format i.e MM/DD/YYYY
String strTest = sc.D_ob;
String[] arrTest = strTest.split('-');
String datetosend=arrTest[1]+'/'+arrTest[2]+'/'+arrTest[0];
sc.D_ob=datetosend;
//Parsed into Date
st.DOB__c= date.parse(sc.D_ob);
}
@RemoteAction
public static Student__c fetchRecord()
{
Student__c student=[
SELECT DOB__c
FROM Student__c
order by CreatedDate desc limit 1
];
System.debug(student);
return student;
}
//VisualForce Page
function fetchRecord() {
MyCustomController1.fetchRecord(handlefetchStudent);
}
function handlefetchStudent(result,event){
if(event.type == 'exception') {
alert(event.message);
} else {
console.log(result.DOB__c.toString());
}
//but date stored as 1/1/2017 in salesforce org i want same date how to do it?
//Output on console=1483228800000
Best Answer
The field is being written out in epoch time (the number of millseconds since January 1st, 1970). To get it back to a normal date, use the Date constructor:
You could have also avoided this issue by serializing the result into JSON before returning, and deserializing afterwards: