Age calculation using Apex and Visualforce

Sample code:

Visualforce page:

<apex:page controller="Sample" >
<apex:form >
    <apex:pageblock >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Date:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext onfocus="DatePicker.pickDate(true, this , false);" value="{!dat}" id="dt"/></apex:pageblockSectionItem>
            <apex:pageblockSectionItem >Age:</apex:pageblockSectionItem>                       
            <apex:pageblockSectionItem ><apex:outputText value="{!age}" /></apex:pageblockSectionItem>           
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Find" action="{!find}"/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>   
</apex:page>


Apex Controller:

public class Sample
{  
    public Integer age {get;set;}    
    public Date dat {get;set;}
   
    public void find()
    {
        Integer temp = dat.daysBetween(Date.Today());
        age = Integer.valueOf(temp/365);
    }
}


Output:

5 comments:

  1. this not working fine please check onec

    ReplyDelete
  2. How did you got to know about , onfocus="DatePicker.pickDate(true, this , false);"....where can we find help on this

    ReplyDelete