jmasterx
jmasterx

Reputation: 54113

Getting hours occupied in a day

Say I have a variable DayHours and a DateTime called CurrentDay.

I have events with a start date, an end date, and hours. They all fall within CurrentDay If it is the last day of the event and the end date == CurrentDay, then I need the remainder. So if a day lasts 5 hours and an event is 14 hours and today is the last day, I would return 4.

If the event starts and ends on the same day, I return its hours. If an event is multiple days and CurrentDay is not the last day of the event, I return DayHours.

How could I do this in C#?

Upvotes: 1

Views: 94

Answers (1)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

You gave the logic for it already:

if (StartDate.Date == EndDate.Date) { return Hours; }
else if (EndDate.Date != CurrentDay.Date) { return DayHours; }
else if (StartDate.Date <= CurrentDate.Date && EndDate.Date > CurrentDate.Date) { return Hours % DayHours; }
else return 0;

The variables may change, depending on what your actual code looks like, but this uses the data you say you are given.

Upvotes: 2

Related Questions