2

I'm having trouble passing a JavaScript date type var as the apex param value. The following is a sample code segment...

<script>
    var today = new Date(93740000);

    <apex:outputText value="{0,date,yyyy',' M',' d}">
        <apex:param value="today" />
    </apex:outputText>
</script>

This gives me the error:

Error: The value attribute on is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice.

How can I resolve this issue and use the JavaScript date in Visualforce code?

4

JavaScript is client side, while apex code renders server side. In other words, Salesforce tries to convert the literal value of "today" into a date using valueOf, which fails, because it's not a valid formatted date.

You have a few choices here, depending on the exact usage you're going for. Perhaps the easiest would be to store the value in a hidden field:

<apex:inputHidden id="DateVal" value="{!myDateField}" />

You can reference this in Apex Code, and it'll be available in the controller on the next round trip (e.g. by using an action function). There must be a public getter and setter that matches the value in the input field for this to work. I'd recommend using a number type if you plan on using Epoch Time (number of milliseconds since 1970).

Other variations to this technique exist, but you'll need a server side variable to get Visualforce to read your date value

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.