+ Start a Discussion
Vinita KumariVinita Kumari 

How to month in number from "MMM" format?

I'm working on a trigger where i need to convert month name to integer value and i dont want to hard code like 'jan' = 1. Plz suggest.
Best Answer chosen by Vinita Kumari
Deepak GulianDeepak Gulian
DateTime d = datetime.now();
Integer count = 1;
Integer dtMonth = 0;
while (count < 13) {
     String monthName = d.format('MMMMM');
     monthName = monthName.substring(0,3);
      If(monthName == 'Compare your picklist value'){
            dtMonth = d.month();
            System.debug(dtMonth);
            break;    
     }
     else{
         d = d.addMonths(1);
         count++;
     }
}       

All Answers

Deepak GulianDeepak Gulian
Integer dtMonth = System.today().month();
If this wont help you out just provide me the exact format of date which you currently have in your trigger.
sivaextsivaext
this link helps. you need to define in helper class and passes as string parameter.
https://developer.salesforce.com/forums/?id=906F00000008ugkIAA
Vinita KumariVinita Kumari
Hi Deepak 
There is no date format.
I have picklist of 'Jan', 'Feb', 'Mar',etc from which I need to get the month in number.
Deepak GulianDeepak Gulian
DateTime d = datetime.now();
Integer count = 1;
Integer dtMonth = 0;
while (count < 13) {
     String monthName = d.format('MMMMM');
     monthName = monthName.substring(0,3);
      If(monthName == 'Compare your picklist value'){
            dtMonth = d.month();
            System.debug(dtMonth);
            break;    
     }
     else{
         d = d.addMonths(1);
         count++;
     }
}       
This was selected as the best answer
Vinita KumariVinita Kumari
Thanks! It worked for me.