+ Start a Discussion
anupama.aj21.3944428619426914E12anupama.aj21.3944428619426914E12 

How to get the value of date picker inputtext in apex controller?

I have a requirement where i need to get the date from the datepicker field and convert that into standart GMT format (yyyy-MM-ddTHH:mm:ss.SSSZ).

how do I get the value of the selected date from the datepicker field?
below is my code .

VF Page :
-----------
<apex:page controller="datePicker1" id="mypage">
    <apex:form >  
        Date: <apex:inputText value="{!datename}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" />  
        <apex:commandButton action="{!displayDate}" value="dispdate" />
        {!outputdate}
    </apex:form>
</apex:page>

APEX Code :
-------------------------

public class datePicker1
{

    public String outputdate { get; set; }
     public datetime datename {get; set;}
    
     public void displayDate()
     {
        
         String formattedDate = datename.format('yyyy-mm-dd');
         outputdate = formattedDate;
        
    }
}

This code is not displaying any thing please help to get the converted date.
Salesforce_krprSalesforce_krpr
Try this

Visualforce Page
-----------------------------------

<apex:page controller="datePicker" id="mypage">
    <apex:form >
        Date: <apex:inputText value="{!dateName}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" />
        <apex:outputpanel id="counter">                       
            <apex:outputText value="Click Me!"/>
            <apex:outputText value="The formatted time right now is: {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}">
               <apex:param value="{!dateName}" />
           </apex:outputText>
            <apex:actionSupport event="onclick" rerender="counter" status="counterStatus"/>
        </apex:outputpanel>
    </apex:form>
</apex:page>

Visualforce Controller
-------------------------------

public with sharing class datePicker {

    public Date dateName{ get; set; }

}
RitikaBhrgvRitikaBhrgv
Hi Anupama,

Below is a version of code. I havent tried it, but I think it should bind the variable to the controller.

Controller:

public class datePicker1
{
    public String outputdate { get; set; }
    public datetime datename {get; set;}
   
    public void displayDate()
    {
        String formattedDate = outputdate + ' 00:00:00';
        datename = Datetime.valueOfGmt(formattedDate.replaceAll('/', '-'));
    }
}

Visualforce Page:

<apex:page controller="datePicker1" id="mypage">
    <apex:form>
     <apex:outputPanel id="theOP">
         Date: <apex:inputText value="{!outputdate}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" /> 
   <apex:commandButton action="{!displayDate}" value="dispdate" rerender="theOP"/>
         {!outputdate}
     </apex:outputPanel>
    </apex:form>
</apex:page>

The issue was that you were trying to bind a datetime field directly to the inputText, which was giving a validation error and hence not calling the controller method.

I have also mentioned an outputPanel which is rerendered on button click. This prevents the rerendering of the VF page.

Hope this helps.
learning1.3953795511514497E12learning1.3953795511514497E12
Hi RitikaBhrgv,

Ur Idea worked with minor changes,

But it is displaying the date in " Sun Oct 05 00:00:00 GMT 10 " format. Where as I need it in " yyyy-MM-ddTHH:mm:ss.SSSZ " format.
Please share if u have any idea.
learning1.3953795511514497E12learning1.3953795511514497E12
Also I have tried with the following....

       String formattedDate = datename + ' 00:00:00';      
       datetime a = DateTime.valueOfGmt(formattedDate.replaceAll('/', '-'));
      
       outputdate =  a.format('yyyy-MM-ddTHH:mm:ss.SSSZ');   // This gives the error : Un recognized format
       outputdate =  a.format('yyyy-MM-dd'); // and this is changing the actual date itself. eg: if my actual date is 3/21/2014 then it is showing 0010-03-06