My question is different than the other date questions Ive seen here. I'm trying to calculate all of the dates using Day, Month and Year inputs. Right now I'm using a Custom Setting but think might be able to use Apex which would be much better. Needs to include leap years of course.

Input fields: Day, Month, Year Ex: Monday, June, 2013

Expected Output: June 3, 2013 June 10, 2013 June 17, 2013 June 24, 2013

public class myDateClass{

    static map<string,integer> dowMap = new map<string,integer>{'Monday'=>0,

    public static list<date> getMyDates(string dayOfWeekName, integer month, integer year){
        list<date> returnList = new list<date>();
        date d = date.newInstance(year,month,1);
        d=d.addDays(math.mod(7-dayOfWeek(d)+dowMap.get(dayOfWeekName),7));//brings you to the first instance of the specified dayOfWeekName of the month

        } while(d.month()==month);

        return returnList;

    public static integer dayOfWeek(date d){
        return math.mod(date.newInstance(1900,1,8).daysBetween(d),7); 


list<date> dates = myDateClass.getMyDates('Monday',4,2013);
for(date d:dates)


15:53:22:067 USER_DEBUG [3]|DEBUG|2013-04-01 00:00:00
15:53:22:067 USER_DEBUG [3]|DEBUG|2013-04-08 00:00:00
15:53:22:067 USER_DEBUG [3]|DEBUG|2013-04-15 00:00:00
15:53:22:067 USER_DEBUG [3]|DEBUG|2013-04-22 00:00:00
15:53:22:068 USER_DEBUG [3]|DEBUG|2013-04-29 00:00:00

If you need a special date format in a Visualforce page, you can use apex:outputText


<apex:outputText value="{0,date,EEEE MM'/'dd'/'yyyy}">
    <apex:param value="{!myDate}" /> 


Wednesday 6/26/2013
  • Yes I'm familiar with each of those but question is figuring out the dates with the input listed. Different than what you have listed. – ddeve Jun 26 '13 at 17:36
  • well, what's your formula for determining those dates? is your question what are all the days in a month that lie on a specific day of the week? – Scott Pelak Jun 26 '13 at 19:15
  • post edited. is this what you were looking for? – Scott Pelak Jun 26 '13 at 23:01
  • This edited myDateclass seems to be exactly it! – ddeve Jun 27 '13 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.