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
jojoforcejojoforce 

How to pass an sObject that has a Date field in JavaScript Remoting?

I have a Visualforce Page that is using JavaScript remoting to essentially pass an sObject that has a Date Field into a JavaScript remoting action. However, it appears that it does not seem to recognize the javascript date format. 

 
function MyObject__c(){
    this.Name = null; 
    this.Start_Date__c = null;

}


function createRecord() {
        MyObject__c myobj = new MyObject__c();

        myobj.Start_Date__c  = '2017-01-01'; 

        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.myclass.mymethod}',
            myobj,
            function(results, event){
                if (event.status) {
                
                	console.log(results);


                }
             }, 
             {escape: true}
         );		
}

I have tried different variation, but it's not working. Any advice would be great.

Variations that I tried...
myobj.Start_Date__c = new date('2017-01-01').toUTCString();

Also this one...
myobj.Start_Date__c = new date('2017-01-01');

And this one...
 
myobj.Start_Date__c = new date('2017-01-01').toISOString();

 
Alain CabonAlain Cabon
Hi,
 
function createRecord() {
       
       var mydate = Date.parse("2017-01-01");
       var myname = "TOTO";
    
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.myclass.mymethod}',
            { "Name": myname,"Start_Date__c":mydate}, 
            function(results, event){
                if (event.status) {              
                    console.log(results);
                }
             }, 
             {escape: true}
         );        
}

Regards
Alain
Alain CabonAlain Cabon
Hello Jojo,

Date.parse()​:   The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

Direct call:  Date.parse(dateString)
Implicit call:  new Date(dateString)

A string representing an RFC2822 or (a variant of) ISO 8601 date (other formats may be used, but results may be unexpected).

The parse() method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. 

https://tools.ietf.org/html/rfc2822#section-3.3​

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse