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
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.
 
Subin Babu 15Subin Babu 15
public String returnMonthNumber(String monthName)
{
    Map<String, String> monthsMap = new Map<String, String>
    {
        'January'  => '01',
        'February' => '02',
        'March' => '03',
        'April' => '04',
        'May' => '05',
        'June' => '06',
        'July' => '07',
        'August' => '08',
        'September' => '09',
        'October' => '10',
        'November' => '11',
        'December' => '12',
        'Jan'  => '01',
        'Feb' => '02',
        'Mar' => '03',
        'Apr' => '04',
        'Jun' => '06',
        'Jul' => '07',
        'Aug' => '08',
        'Sep' => '09',
        'Oct' => '10',`
        'Nov' => '11',
        'Dec' => '12'

      };
      return(monthsMap.get(monthName));
}