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?

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.