1

How can I create an apex input with data type date?

I tried using showDatePicker but it has an error:

value for <apex:inputField> is not a dynamic binding.

2 Answers 2

3

There are several ways in which you can achieve this.

If you want to mimic datepicker which is seen in salesforce UI, the simplest one is as suggested by @Boris in his answer.

The other ways include:

  1. Use HTML input tag with attribute as type="date"

    <input type="date" name="bday">

  2. Use HTML input tag and any javascript library like jQuery, ExtJs etc. The below example is for jQuery. You can use jQuery datepicker() function to turn a text box into datepicker.

    <input type="text" id="datepicker">

    <script> $(function(){ $("#datepicker" ).datepicker(); }); </script>

  3. Use salesforce apex:inputText tag and any javascript library like jQuery, ExtJs.

    <apex:inputtext id="datePicker" onclick="openDatePicker()"/>
       <script>
        var j$ = jQuery.noConflict();
        function openDatePicker(){                                
            var dtpckr= document.getElementById('{!$Component.datePicker}');
            j$(dtpckr).datepicker();
        }
       </script>
    
2

If I understood the question correctly (input field date), you're referring to VisualForce.

You can create a temporary instance of an sObject which has a date field, for an example Opportunity which has a field CloseDate.

So in your controller add:

public Opportunity oppty {get; set;}

Then inside your constructor or init method create a new instance:

oppty = new Opportunity();

Finally in your VisualForce page you can use it as:

<apex:inputField value="{!oppty.CloseDate}" />

Then whatever you do in your controller, you can always reference the value in oppty.CloseDate.

This is one of the quickest but not cleanest solutions.

1
  • I am not using Opportunity object but have to use it in order to enable <apex:inputField> .This seem little dirty but solve the issue instead of waiting this feature to be available by salesforce.
    – unidha
    Commented Apr 12, 2017 at 7:02

You must log in to answer this question.

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