int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind

int days, int hours, int minutes, int seconds, int milliseconds

public

TimeSpan Subtract(

DateTime value

)

// Working Algorithm

If FromDate > ToDate Then:

return

0;

var timeDifference = ToDate.Subtract(FromDate)

while

FromDate < ToDate:

If FromDate == Saturday OR FromDate == Sunday Then:

timeDifference = timeDifference.Subtract(timeSpanOfOneDay)

If FromDate == BankHoliday

FromDate = FromDate.AddDays(1)

timeDifference

// A dummy date of 01/01/1900 00:00:00 is created

DateTime dummy =

new

DateTime(1900, 1, 1, 0, 0, 0);

DateTime nextDummyDay = dummy.AddDays(1);

// AddDays() to add number of days

DaySpan ComputeDaysDifference(DateTime ToDate, DateTime FromDate, DateTime[] BankHolidays,

bool

WorkOnSaturday =

false

,

WorkOnSunday =

{

DateTime TDate =

DateTime(FromDate.Year, FromDate.Month, FromDate.Day, FromDate.Hour, FromDate.Minute, FromDate.Second, FromDate.Millisecond);

if

(ToDate.Year == 1900)

ToDate = DateTime.Now;

}

int

DaysOverdue = 0;

HoursOverdue = 0;

MinsOverdue = 0;

SecsOverdue = 0;

// Time Difference between FromDate and ToDate in TimeSpan format

TimeSpan difference = ToDate.Subtract(FromDate);

// subtract the bank holidays

DateTime nextDummyHour = dummy.AddHours(1);

// AddHours() to add number of hours

TimeSpan timespanOfOneDay = nextDummyDay.Subtract(dummy);

TimeSpan timespanOfOneHour = nextDummyHour.Subtract(dummy);

try

(FromDate > ToDate)

DaySpan();

// not overdue

DateTime intermediate =

DateTime(FromDate.Year, FromDate.Month, FromDate.Day);

(intermediate < ToDate)

(intermediate.DayOfWeek == DayOfWeek.Saturday)

(!WorkOnSaturday)

difference = difference - timespanOfOneDay;

(intermediate.DayOfWeek == DayOfWeek.Sunday)

(!WorkOnSunday)

intermediate = intermediate.AddDays(1);

(BankHolidays !=

null

&& BankHolidays.Length > 0)

for

(

i = 0; i < BankHolidays.Length; i++)

(BankHolidays[i].Ticks > FromDate.Ticks && BankHolidays[i].Ticks < ToDate.Ticks)

// Bank holidays is in between FromDate and ToDate

catch

(Exception ex)

string

error =

"Failed to compute difference in days for Completion Date "

+ ToDate.ToString() +

" and Target "

+ FromDate.ToString()

+

". Error:"

+ ex.Message.ToString();

throw

Exception(error);

finally

DaysOverdue = difference.Days;

HoursOverdue = difference.Hours;

MinsOverdue = difference.Minutes;

SecsOverdue = difference.Seconds;

DaySpan { Days = DaysOverdue, Hours = HoursOverdue, Minutes = MinsOverdue, Seconds = SecsOverdue };

// Custom DaySpan to be returned by ComputeDaysDifference()

struct

DaySpan

Days;

Hours;

Minutes;

Seconds;

static

void

Main(

[] args)

TimeDifference td =

TimeDifference();

DateTime fromDate =

DateTime(1990, 12, 13);

DateTime toDate =

DateTime(1990, 12, 14);

// Difference of one day

var dayDifference = td.ComputeDaysDifference(toDate, fromDate,

);

Console.WriteLine(

"Case 1"

"Day Difference: "

+ dayDifference.Days);

"Hour Difference: "

+ dayDifference.Hours);

"Minute Difference: "

+ dayDifference.Minutes);

"Seconds Difference: "

+ dayDifference.Seconds);

"\n"

// Difference of one day and seven hours

fromDate =

DateTime(1990, 12, 13, 6, 0, 0);

toDate =

DateTime(1990, 12, 14, 13, 0, 0);

dayDifference = td.ComputeDaysDifference(toDate, fromDate,

"Case 2"

// Difference of two days and seven hours. There's one Saturday and Sunday in between

DateTime(1990, 12, 17, 13, 0, 0);

"Case 3"

// Difference of three days and seven hours. There's one Saturday and Sunday in between.

// We work on Saturday and hence it is true.

true

"Case 4"

// Difference of one day and seven hours. There's one Saturday and Sunday in between.

// We work on Saturday and hence it is true. And 14th and 17th are holidays.

DateTime[] holidays = {

DateTime(1990, 12, 14),

DateTime(1990, 12, 17)};

dayDifference = td.ComputeDaysDifference(toDate, fromDate, holidays,

"Case 5"