+ Start a Discussion
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan 

How to get the date and month values through Apex trigger?

Hi All,

 

I'm in need of splitting the Date field value into Date, Month and Year through trigger.

 

My one part of requirment is to update a text field value as 'Oct 7', If you the date field value is given as '10/7/2013'. Please hepls me in finding a solution for this.

 

Thanks in Advance...!

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Thanks for your help. I have found the solution. Your link also helped me at the right time.

 

Following is the way to check the date and month from a date field value.

 

public static string getMonthEnd(date thisdate ){
       String month;
            Integer dt =  thisdate.day();
            Integer dtMonth = thisdate.month();            
            if(dtMonth == 1){
              month = 'Jan'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 2){
              month = 'Feb'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 3){
              month = 'Mar'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 4){
              month = 'Apr'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 5){
              month = 'May'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 6){
              month = 'Jun'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 7){
              month = 'Jul'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 8){
              month = 'Aug'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 9){
              month = 'Sep'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 10){
              month = 'Oct'+' '+ string.ValueOf(dt);
            }            
            if(dtMonth == 11){
              month = 'Nov'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 12){
              month = 'Dec'+' '+ string.ValueOf(dt);
            }
        return month;
    }

 

It will return the string  'Oct 7' as the output. This might help others as well for future references. So, I'm marking this as a solution.

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Thanks for your help. I have found the solution. Your link also helped me at the right time.

 

Following is the way to check the date and month from a date field value.

 

public static string getMonthEnd(date thisdate ){
       String month;
            Integer dt =  thisdate.day();
            Integer dtMonth = thisdate.month();            
            if(dtMonth == 1){
              month = 'Jan'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 2){
              month = 'Feb'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 3){
              month = 'Mar'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 4){
              month = 'Apr'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 5){
              month = 'May'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 6){
              month = 'Jun'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 7){
              month = 'Jul'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 8){
              month = 'Aug'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 9){
              month = 'Sep'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 10){
              month = 'Oct'+' '+ string.ValueOf(dt);
            }            
            if(dtMonth == 11){
              month = 'Nov'+' '+ string.ValueOf(dt);
            }
            if(dtMonth == 12){
              month = 'Dec'+' '+ string.ValueOf(dt);
            }
        return month;
    }

 

It will return the string  'Oct 7' as the output. This might help others as well for future references. So, I'm marking this as a solution.

This was selected as the best answer