
For example if I have a date of 08/06/2018 and 3 number of weeks, then I would want to generate the dates of the mondays in that range which should be 08/06/2018, 08/13/2018 and 08/20/2018.
Can anyone help me?
Veronica
2 Antworten
Hi, Using an arbitary date of Jan 6 1900 as the beginning of time, the formula below
Math.mod(BeginningOfTimeDate.daysBetween(startDate), 7)
Would return
- 0 if date__c is saturday
- 1, sunday
- 2, monday
- ...
- 6, friday
As such, below is an apex code that outputs 3 Mondays
Date basis = Date.newInstance(1900,1,6); // beginning of time
Date start = Date.newInstance(2018, 8,1); // start date, input
Integer num = 3; // number of dates to retrieve, input
for(integer i=1; i<=3; i++) {
//adding days to start date until we get to a Monday
while(2!=Math.mod(basis.daysBetween(start), 7)) {
start=start.addDays(1);
}
// if Monday, display date then add 7 days
if(2==Math.mod(basis.daysBetween(start), 7)) {
system.debug(start);
start=start.addDays(7);
}
}
Hope that helps!