WorkCalendar
A working calendar: which days of the week are worked and which dates are not, and the conversion between calendar dates and the count of working days the scheduler plans in.
public sealed class WorkCalendar
Remarks
What it is for
Every duration and lag in a plan is in working days. The scheduler numbers the working days
from the project's first one – index 0 is Origin, index 1 the next working day, and
so on, with weekends and holidays simply not on the count – and this class turns those indexes
into dates and back. The calendar a plan was scheduled on is
ProjectSchedule.Calendar; use it to answer questions such as "what date is ten
working days after this task finishes?" in exactly the terms the charts used.
Dates that are not worked
IndexOf() and DateAt are exact inverses on working days.
IndexOf() on a weekend or holiday answers the next working day's index, which
is what a planned start on a Saturday means. A calendar with no working days at all is read as
every day being worked, so that no task is ever infinitely long.
Dates carry no time of day and no time zone: only the date part of a
DateTime is read.
Examples
A calendar over a UK Christmas, with the bank holidays taken out.
using System;
WorkCalendar calendar = new WorkCalendar(
new DateTime(2026, 12, 21),
WorkCalendar.DefaultWorkingDays,
new[] { new DateTime(2026, 12, 25), new DateTime(2026, 12, 28), new DateTime(2027, 1, 1) });
bool open = calendar.IsWorkingDay(new DateTime(2026, 12, 28)); // false: a bank holiday
int index = calendar.IndexOf(new DateTime(2026, 12, 29)); // 4: the fifth working day
DateTime back = calendar.DateAt(7); // Monday 4 January 2027
int days = calendar.WorkingDaysBetween(new DateTime(2026, 12, 21),
new DateTime(2027, 1, 8)); // 12See also
Members at a glance
| Constructors | |
|---|---|
WorkCalendar | Creates a calendar whose first working day – index 0 – is origin, or the next
working day after it. |
| Methods | |
DateAt | Converts a working-day index to its date. |
IndexOf | Converts a date to its position in the count of working days, rounding a weekend or holiday forward to the next working day. |
IsWorkingDay | Tells whether a date is worked: its day of the week is a working day and it is not a holiday. |
ToDate | Gets the first working day: working-day index 0. |
WorkingDaysBetween | Counts the working days in a range of dates, both ends included. |
| Fields | |
DefaultWorkingDays | The working week used when none is given: Monday to Friday. |
Constructors
WorkCalendar
Creates a calendar whose first working day – index 0 – is origin, or the next
working day after it.
Parameters
origin- The first day work could happen; only its date part is read.
workingDays- The days of the week that are worked. Null gives
DefaultWorkingDays; a list with no valid day in it, such as an empty one, gives every day of the week. holidays- Dates that are not worked, whatever day of the week they fall on; null for none.
Methods
DateAt
Converts a working-day index to its date.
Parameters
index- The index: 0 for
Origin, positive after it, negative counting back before it.
Returns
The working day at that position, with no time of day.
Example
// The date ten working days after a task finishes, on the plan's own calendar.
ScheduledTask api = schedule.Find("api");
DateTime chaseDate = schedule.Calendar.DateAt(api.EarlyFinish + 9);IndexOf
Converts a date to its position in the count of working days, rounding a weekend or holiday forward to the next working day.
Parameters
date- The date; its time of day is ignored. Any date, before or after
Origin.
Returns
Its working-day index: 0 for Origin, negative before it. A non-working day
before the origin rounds towards the origin, and one just before it answers 0.
IsWorkingDay
Tells whether a date is worked: its day of the week is a working day and it is not a holiday.
Parameters
date- The date; its time of day is ignored.
Returns
true for a working day.
ToDate
Gets the first working day: working-day index 0.
- Value
- The origin given to the constructor, or the first working day after it when it is not worked.
WorkingDaysBetween
Counts the working days in a range of dates, both ends included.
Parameters
from- The first day of the range.
to- The last day of the range.
Returns
The number of working days from from to to
inclusive; zero when to is earlier than from.
Fields
DefaultWorkingDays static
The working week used when none is given: Monday to Friday.