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
siva@shiva.comsiva@shiva.com 

How to get Month from date field in apex class

just i want to retrive month from dateofbirth field ...please help me

Ritesh AswaneyRitesh Aswaney

Contact con = [Select Id, BirthDate from Contact where Birthdate != null LIMIT 1];
System.debug(con.BirthDate.Month());

siva@shiva.comsiva@shiva.com

it gives an error ...........

let me clear i want to get records from accountbased on date of birth ,and my vf page contain  month pick list .if i select picklist value then display releted records

 

<apex:page controller="monthtest">
  <apex:form >
 Select Month <apex:selectList value="{!pickval}" size="1" >
 
 <apex:selectOptions value="{!monthname}" ></apex:selectOptions>
  </apex:selectList>
 
  <apex:commandButton value="go" action="{!Go}"/>
  </apex:form>
</apex:page>

 

 

----------

class

 

public with sharing class monthtest {

account  acc= [Select Id,dob__c from account where dob__c != null LIMIT 1];

System.debug(con.BirthDate.Month());



     public PageReference Go() {
        return null;
    }
    
    public String pickval { get; set; }
    public  list<selectoption> getmonthname()
    {
    list<selectoption> month=new list<selectoption>();
    month.add(new selectoption('jan','jan'));
    month.add(new selectoption('feb','feb'));
    return month;
    }  
    
   
}

Ritesh AswaneyRitesh Aswaney
It would be easier to store the month as as a formula on your account to query against directly. So assuming your dob__c is a DateField Create a formula dob_month__c Month(dob__c) Then in your query, just query account where month is the selected month from your picklist I suspect the Month function yields a numerical value like 6 as opposed to Jun so you might want to have the value of your selectOption as the month number and the label as the name. Sent from my iPhone