Tuesday 13 March 2012

Find 'Day' & ‘Day Of Week’ From 'Date' in Salesforce.

There are many scenarios in salesforce where we need to find out day from date.
For example, for scheduling a job through system.schedule() method , we need  Day_of_week.





Date startDate = date.newInstance(0001, 1, 1);

List<String> daysList = new List<String>
{'null', 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday','Saturday' }; 


Date dayForDate = system.today();

Integer remainder = Math.mod(startDate.daysBetween (dayForDate) , 7);
       

if(remainder==0){remainder=7;}
       

string calculatedDay = daysList.get(remainder);

system.debug('Day of week for dayForDate ---->'+remainder);


system.debug('day for dayForDate ----->'+ calculatedDay);

No comments: