I have a requirement to obtain number of days passed since creation date. This number would need to minus the weekends. I have only some functions : JulianDay, JulianWeek, JulianYear to get Julian date values, I also have Today which returns the date of today, time stamp which returns date and time. I have manage to get the difference of today-creation date by using: JulianDay(today)-JulianDay(creation date) but I still can't wrap my head around subtracting the weekends
-
1Which framework/language are you working with? It might turn out to be irrelevant, but it could help others point out date APIs that already exist and might help you in this regard. – Jasarien Jul 04 '13 at 15:52
-
The reason why I didn't state is because I'm working on a calculated field on Siebel but I also thought an algorithm of such would be useful – acvon Jul 04 '13 at 16:00
-
oh, now I understand. Mi answer was based thinking that you where using java. You basically need to loop through all the days and ask if the current date is sunday or saturday, if so, then increment a counter. – David Hofmann Jul 04 '13 at 16:03
-
What exactly does JulianDay return? If it returns a number from 1 to 31, the `JulianDay(today) - JulianDay(creation date)` calculation will run into trouble if the dates occur in two different months (or years)... – DPenner1 Jul 04 '13 at 17:34
-
Also see questions [1](http://stackoverflow.com/questions/2001765) [2](http://stackoverflow.com/questions/12932965) [3](http://stackoverflow.com/questions/10330836) [4](http://stackoverflow.com/questions/13792548) [5](http://stackoverflow.com/questions/279296) [6](http://stackoverflow.com/questions/9757919) [7](http://stackoverflow.com/questions/7388420) as linked in right sidebar – James Waldby - jwpat7 Jul 04 '13 at 23:00
3 Answers
Not completely sure what the functions you cited in your question do, however, you seem to be comfortable with doing the basic date arithmetic to determine the number of days between two given dates. The hard part seems to be figuring out how may days to subtract for weekends.
I think you can accomplish this with two functions:
- Given two dates, return the number of days between them. Call this
DAYS(date-1, date-2)
- Given a date, return the day of the week (where 1 = Monday ... 7 = Sunday). Call this
DAY-OF-WEEK(date)
Having these functions you can then do the following:
- Calculate full weeks in the date range:
WEEKS = DAYS(date-1, date2) mod 7
- Calculate days not parts of full weeks:
DAYS-LEFT = DAYS(date-1, date-2) - (WEEKS * 7)
- Determine which day of the week the last day falls on:
LAST-DAY = DAY-OF-WEEK(date-2)
Adjust the number of DAYS-LEFT
from the partial week as follows:
if DAYS-LEFT > 0 then
case LAST-DAY
when 6 then /* Saturday */
DAYS-LEFT = DAYS-LEFT - 1
when 7 then /* Sunday */
if DAYS-LEFT = 1 then
DAYS-LEFT = 0
else
DAYS-LEFT = DAYS-LEFT - 2
end-if
when other /* Monday through Friday */
case DAYS-LEFT - LAST-DAY
when > 1 then
DAYS-LEFT = DAYS-LEFT - 2
when = 1 then
DAYS-LEFT = DAYS-LEFT - 1
when other
DAYS-LEFT = DAYS-LEFT /* no adjustment */
end-case
end-case
end-if
DAYS-EXCLUDING-WEEKENDS = DAYS(date-1, date-2) - (WEEKS * 2) + DAYS-LEFT
I assume you have, or can build, a DAYS(date-1, date-2)
function. The next bit is to determine what day of the week
a given date falls on. The algorithm to do this is called Zeller's congruence. I won't
repeat the algorithm here since Wikipedia does a fine job of describing it.
Hope this gets you on your way...

- 16,670
- 2
- 39
- 60
-
This seems to be a good algorithm but I have just found out in my context, I cannot use % :( so much limitation! – acvon Jul 05 '13 at 02:46
Your JulianDay(y,m,d)
function returns a serial number for each date; let's say for the sake of discussion that JulianDay(2013,7,4)
returns 2456478. The next day will be 2456479, then 2456480, and so on. And let's say that the difference of two days is diff.
The number of full weeks in diff, each containing 5 weekdays, is diff // 7
(that's integer division, so it rounds down). Thus if diff is 25, there will be 25 // 7 = 3
full weeks plus an extra diff % 7 = 4
days. The 3 full weeks contain 15 weekdays; it doesn't matter which day of the week you start from. So you only need to consider the 4 extra days to see how may are weekdays.
The number that the JulianDay
function returns can be taken modulo 7 to calculate the day of the week; on my JulianDay
function, modulo 5 represents Saturday and modulo 6 represents Sunday. You can take the 4 extra days to be either the 4 days at the beginning of the period or the 4 days at the end; it doesn't matter because all the other days are part of a period of consecutive full weeks that each have 5 weekdays. Say you pick the first 4 days. Then take the JulianDay
of the first day modulo 7, then the JulianDay
of the first day plus 1 modulo 7, then the JulianDay
of the first day plus 2 modulo 7, then the JulianDay
of the first day plus 3 modulo 7, determine how many of them are weekdays, and add that to the number of weekdays in full weeks.
All you need is a JulianDay
function.

- 17,381
- 4
- 34
- 59
This code should do what you want:
Date fromDate = new Date(System.currentTimeMillis()-(30L*24*60*60*1000)); // 30 days ago
Date toDate = new Date(System.currentTimeMillis()); // now
Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
int countDays = 0;
while (toDate.compareTo(cal.getTime()) > 0) {
if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
countDays++;
cal.add(Calendar.DATE, 1);
}
System.out.println(countDays);

- 5,683
- 12
- 50
- 78
-
Thanks for the answer but the thing with this, Siebel is that it's so limited with what I can do where this function is needed hence I don't have the luxury of such functions. Only the functions mentioned are available – acvon Jul 04 '13 at 16:16