Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Sabrent

# How many Fridays between 2 dates?

Is there a  way to find how many Fridays's there are between startdate and enddate?

kirkevonphilly

Hi rov,

Try this:

```date calcDate = date.newinstance(1900, 1, 7);  //  Date used in formula
date yourStartDate = date.newinstance(2012, 11, 1);  // Input start date
date yourEndDate = date.newinstance(2012, 12, 2);  // Input end date

integer friCount = 0;

do {
//  Calc the mod between mod and 1/7/1900     //  1/7/1900 was a Sunday so...    //  0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;
}
} while (yourStartDate <= yourEndDate);

system.debug('friCount:  ' + friCount);```

kirkevonphilly

Hi rov,

Try this:

```date calcDate = date.newinstance(1900, 1, 7);  //  Date used in formula
date yourStartDate = date.newinstance(2012, 11, 1);  // Input start date
date yourEndDate = date.newinstance(2012, 12, 2);  // Input end date

integer friCount = 0;

do {
//  Calc the mod between mod and 1/7/1900     //  1/7/1900 was a Sunday so...    //  0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;
}
} while (yourStartDate <= yourEndDate);

system.debug('friCount:  ' + friCount);```

This was selected as the best answer
Sabrent

Thanks @ kirkevonphilly for your  help with the solution.  I get the desired result in developer console when i hard code the start date and end date.

When i  use it in my code  I get the following error at the user interface when saving the record. Any suggetsions on how i can avoid the error

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger createTSSCSLA caused an unexpected exception, contact your administrator: createTSSCSLA: System.LimitException: Too many script statements: 200001

```Trigger

if (newTsscActivity.Frequency__c == 'Weekly'){

Date d1 = newTsscActivity.Start_Date_Time__c;
Date d2 = newTsscActivity.End_Date__c;

Integer integernumofweeks = Utility_DateFunctions.getCalendarWeeks(d1,d2);

}```

Util class

```public static integer getCalendarWeeks(date startDate, date endDate) {

date calcDate = date.newinstance(1900, 1, 7);

date yourStartDate = date.newinstance(startDate.year(), startDate.month(),startDate.day());
date yourEndDate = date.newinstance(endDate.year(), endDate.month(),endDate.day());

integer friCount = 0;

do {

//  1/7/1900 was a Sunday so...
//  0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;

}
} while (yourStartDate <= yourEndDate);

system.debug('friCount:  ' + friCount);

return friCount;

}
```

Sabrent

Thank you . I figured out how I can get rid of too many script statements error.

These are the changes I made to my trigger and util class

Trigger

``` if (newTsscActivity.Frequency__c == 'Weekly'){

Date d1 = newTsscActivity.Start_Date_Time__c;
Date d2 = newTsscActivity.End_Date__c;

Date weekStart = d1.toStartofWeek();

Integer integernumofweeks = Utility_DateFunctions.getCalendarWeeks(d1,d2,weekStart);

for(Integer nw=0;nw<integernumofweeks;nw++){

TSSC_Request__c = newTsscActivity.Id
));
}

}```

Util class

``` public static integer getCalendarWeeks(date startDate, date endDate, date wkstartdate) {

date yourStartDate = date.newinstance(startDate.year(), startDate.month(),startDate.day());
date yourEndDate = date.newinstance(endDate.year(), endDate.month(),endDate.day());
date calcDate = date.newinstance(wkstartdate.year(), wkstartdate.month(),wkstartdate.day());

integer friCount = 0;

do {

//  weekstart date is a Sunday...
//  0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;

}
} while (yourStartDate <= yourEndDate);

//system.debug('friCount:  ' + friCount);

return friCount;

}```