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
Aman Gupta 74Aman Gupta 74 

Unable to get value of date in Apex class from VisualForce page's date picker

Hi All,

Visual Force Code:
<apex:input type="date" value="{!date}" styleclass="myStyle"/>

Apex Class Code:
public String date{get;set;}

public string getdate() {
            return date;
        }
    
 public void setdate(string date) {
            this.date= date;
        }

NOTE: I have tried using Date datatype as well instead of string, still unable to get the selected value of date from VF page in Apex Class.

Kindly, help.
Thank!
Aman
Madhukar_HeptarcMadhukar_Heptarc
Hi Aman,

Can please try below code.
VF Code :
========
<apex:page controller="GetDate" docType="html-5.0">
    <center><h1>Account Search Page</h1></center>
    <apex:form>
        Start Date: <apex:input type="date" value="{!startDate}" required="true" />
        End Date: <apex:input type="date" value="{!endDate}" required="true" />
        <apex:commandButton value="GetDate" Action="{!Display}"/>
    </apex:form>
</apex:page>

Apex Class:
=========

public class GetDate 
{
    public  Date startDate{get;set;}
    public  Date endDate{get;set;}
    Public void Display()
    {
        this.startDate = StartDate; 
        this.endDate = endDate;
        System.debug(startDate +'/'+endDate);
    }
    
}

Please le me know if it is useful (or) Any help required.

Thanks& Regards
Madhukar_Heptarc.
 
Ajay K DubediAjay K Dubedi
Hi Aman,
Please try the below test class and let me know if this works for you. If still need modifications do let me know.

Vf Pgae:

<apex:page controller = "DatePickerValue" docType="html-5.0">
    <apex:slds />
    <apex:form>
    <apex:input type="date" value="{!selected_date}" />
        <apex:commandButton value="getDate" action="{!getDate}"/>
        </apex:form>
</apex:page>

Controller:

public class DatePickerValue {
    public Date selected_date{get;set;}
    public void getDate() {
        System.debug('Selected Date'+selected_date);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi