1

I am trying to pass a datetime field to a Javascript function:

onclick="myfunction('{!c.ID}', 
                    '{!c.Start_Date__c}', 
                    '{!c.Reason__c}');"

This passes the default formatted Apex datetime which looks like this: MON Jan 01 14:00:00 GMT 2018

Javascript does not parse this as a date. So I need something formatted in a way that JS recognizes as a date (unless I want to parse it myself).

I have tried:

onclick="myfunction('{!c.ID}', 
                    '{!(c.Start_Date__c).format()}', 
                    '{!c.Reason__c}');"

But that doesn't work. I have also tried added parentheses:

 '{!((c.Start_Date__c).format())}', 

No luck with that either. I am trying to avoid having to parse the string myself in JS. Any suggestions?

1

1 Answer 1

1

You don't need to parse the date yourself; JavaScript will do it for you if you use the new Date constructor.

onclick="myfunction('{!c.ID}', new Date('{!c.Start_Date__c}'),  '{!c.Reason__c}');"
2
  • This doesn't work. I get a syntax error: "Syntax error. Missing '}'". It's basically the same as the second option in my explanation, except your are doing the conversion in the call rather than the JS. It's the {!(c.Start_Date__c).format()} that is causing the problem. That's clearly not the correct syntax and I don't know what is. Apr 7, 2017 at 16:42
  • @SalesForceJedi Sorry, I got a bit copy-paste-happy. Try this new and improved version.
    – sfdcfox
    Apr 7, 2017 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .