I am trying to create a List of date type which contains every Friday of the month in it. So for example, for the month of March, the list would contain the dates of March 3rd, 10th, 17th, 24th and 31st. Is there any standard function that would calculate this for me? Or perhaps a standard object that I could query which is simply a calendar so I could use the date query functions? I am not looking for something like

DAY_IN_WEEK(CreatedDate) = 6

In which it would get every friday from a query, but simply a list of Fridays for that month regardless of querying an object.


While the other approaches listed here work, they are dependent on locale. I would prefer an approach that will work for anyone, anywhere, which you can do by comparing the month start to a known Sunday. For example:

static Date knownSunday = Date.newInstance(2017, 1, 1);
public static List<Date> getFridays(Integer year, Integer month)
    Date monthStart = Date.newInstance(year, month, 1);
    Integer daysAfterSunday = Math.mod(knownSunday.daysBetween(monthStart), 7);
    Date firstFriday = monthStart.addDays(5 - daysAfterSunday);
    if (firstFriday < monthStart)
        firstFriday = firstFriday.addDays(7);

    Datetime pointer = Datetime.newInstance(firstFriday, Time.newInstance(0,0,0,0));
    List<Date> fridays = new List<Date>();
    while (pointer.month() == month)
        pointer = pointer.addDays(7);
    return fridays;

You can test it out in Execute Anonymous with values for this year:

for (Integer month = 1; month < 13; month++)
    system.debug(JSON.serialize(getFridays(2017, month)));













| improve this answer | |

Using this function:

public List<Datetime> fridaysInMonth(Integer month_no, Integer year_no) {
    List<Datetime> days_in_month = new List<Datetime>();
    Datetime d = Datetime.newInstance(Date.newInstance(year_no, month_no, 1), Time.newInstance(0, 0, 0, 0));
    Integer count = 0;
    while (count <= 31) {
        Datetime that_day = d.addDays(count);
        if (that_day.month() != month_no) {

        // format('u') gives you the day in week (Friday being the day 5)
        if (that_day.format('u') == '5') {

    return days_in_month;

System.debug(fridaysInMonth(3, 2017)); gives me the following result:

20:18:56:008 USER_DEBUG [27]|DEBUG|(2017-03-03 03:00:00, 2017-03-10 03:00:00, 2017-03-17 03:00:00, 2017-03-24 03:00:00, 2017-03-31 03:00:00)

Basically it will output only the dates you want. And of course you can change the code to get only the days, for example.

| improve this answer | |
  • 1
    The format('u') is also locale dependent, so in some areas it should start from Sunday and others Monday. The documentation on Datetime.format implies as much, though I haven't tested it. Otherwise I do prefer this approach to the others listed. – Adrian Larson Mar 17 '17 at 0:00

I don't know of any true native methods to retrieve the list but you could do something like this:

List<Date> getFridays(Integer year, Integer month) {
  List<Date> toReturn = new List<Date>();
  DateTime checkDate = Date.newInstance(year, month, 1);
  DateTime endDate = checkDate.addMonths(1);
  while(checkDate < endDate){
    if(checkDate.format('E') == 'Fri'){
      checkdate = checkdate.addDays(7);
     } else {
      checkDate = checkDate.addDays(1);
  return toReturn; 
| improve this answer | |
  • 1
    Locale dependent. This wouldn't work for someone whose language isn't English, for instance. – Adrian Larson Mar 16 '17 at 19:39

you could loop through the dates of the month, using code from this link


Datetime dt = System.now();
Will give you Friday
To get Fri - use dt.format('EEE'); 

see also this link which has a solution that just uses numbers: How Can I Tell the Day of the Week of a Date?

MOD(DATEVALUE( Date_Field__c ) - DATE(1985,7,1),7)
| improve this answer | |

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.