Sunday 28 July 2013

Formatting date field in apex:inputtext

If we would like to have the ability to use a date field in Visualforce without relying on a salesforce or custom object then we can acheive this by using <apex:inputtext>. Below is the code snippet



Visualforce Page:
 <apex:page controller="inputTextDemo" id="mypage">  
      <span class="dateInput dateOnlyInput">  
           <apex:inputText id="inputFieldId" size="12" value="{!DateStringProperty}" style="width:72px;text-align:center;" onclick="DatePicker.pickDate(false, this, false);" onfocus="DatePicker.pickDate(false, this, false);"/>  
      </span>  
 </apex:page>  
Controller:
 public class inputTextDemo{  
      // String Property which is bind to <apex:inputtext>  
      public String DateStringProperty{get;set;}   
      public inputTextDemo(){  
           // Populating field value by today date.  
           DateStringProperty = Date.Today().format(); // Populating field value by today date.  
      }  
      public void insertDateMethod(){  
           Custom_Object__c obj = new Custom_Object__c();  
           // Since value is string and Date field does not take string value so we need to parse this string as Date  
           obj.DateField__c = Date.parse(DateStringProperty);   
           insert obj;  
      }  
 }  

No comments:

Post a Comment