1

The goal is to calculate the total number of leave days taken by all employees, broken down by month and year, ensuring weekends are not included in the count . The summation of leave days is returning incorrect values.

public double CalculateLeaveDaysForPeriod(List<EmployeeInfoDto> staffMembers, DateTime periodStart, DateTime periodEnd)
{
    double totalLeaveDays = 0;

    foreach (var staffMember in staffMembers)
    {
        foreach (var leaveRequest in staffMember.LeaveRequests)
        {
            DateTime leaveStartDate = leaveRequest.StartDate;
            DateTime leaveEndDate = leaveRequest.EndDate;

            DateTime validStartDate = leaveStartDate > periodStart ? leaveStartDate : periodStart;
            DateTime validEndDate = leaveEndDate < periodEnd ? leaveEndDate : periodEnd;

            if (validStartDate < validEndDate)
            {
                for (DateTime currentDay = validStartDate; currentDay <= validEndDate; currentDay = currentDay.AddDays(1))
                {
                    if (currentDay.DayOfWeek != DayOfWeek.Saturday && currentDay.DayOfWeek != DayOfWeek.Sunday)
                    {
                        totalLeaveDays++;
                    }
                }
            }
        }
    }
    return totalLeaveDays;
}
 public double GetLeaveDaysForCurrentMonth(List<EmployeeInfoDto> employees)
{
    var currentDate = DateTime.Now;
    var startOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1); 
    var endOfMonth = startOfMonth.AddMonths(1).AddDays(-1); 

    return GetLeaveDaysForTimePeriod(employees, startOfMonth, endOfMonth);
}

public double GetLeaveDaysForCurrentYear(List<EmployeeInfoDto> employees)
{
    var currentDate = DateTime.Now;
    var startOfYear = new DateTime(currentDate.Year, 1, 1); 
    var endOfYear = new DateTime(currentDate.Year, 12, 31); 

    return GetLeaveDaysForTimePeriod(employees, startOfYear, endOfYear);
} 

Here’s an example of the JSON data I’m working with, showing the start and end dates for each employee:

 "leaveRequests": [
    {
      "startDate": "2025-01-07T23:00:00Z",
      "endDate": "2025-01-09T23:00:00Z"
    }
  ]
},
{
  "leaveRequests": [
    {
      "startDate": "2025-01-05T23:00:00Z",
      "endDate": "2025-01-17T23:00:00Z"
    }
  ]
},
{
  "leaveRequests": [
    {
      "startDate": "2025-01-08T23:00:00Z",
      "endDate": "2025-01-23T23:00:00Z"
    }
  ]
}
2
  • FYI The DateTime class has this nice static method: DateTime.DaysInMonth(year, month); Commented Jan 10 at 22:19
  • Maybe you should use if (validStartDate <= validEndDate)? Because in the code that follows it, you also use <=. Or if it should stay < then maybe use < in both places. Commented Jan 10 at 22:57

2 Answers 2

0

The time parts may cause you trouble.

This works for me:

//DateTime leaveStartDate = new DateTime(2025, 1, 7, 23, 0, 0);
//DateTime leaveEndDate = new DateTime(2025, 1, 9, 23, 0, 0);

//DateTime leaveStartDate = new DateTime(2025, 1, 5, 23, 0, 0);
//DateTime leaveEndDate = new DateTime(2025, 1, 17, 23, 0, 0);

//DateTime leaveStartDate = new DateTime(2025, 1, 8, 23, 0, 0);
//DateTime leaveEndDate = new DateTime(2025, 1, 23, 23, 0, 0);

DateTime leaveStartDate = new DateTime(2025, 1, 8, 23, 0, 0);
DateTime leaveEndDate = new DateTime(2025, 2, 4, 23, 0, 0);

//DateTime periodStartDate = new DateTime(2025, 1, 1, 0, 0, 0);
//DateTime periodEndDate = new DateTime(2025, 1, 31, 0, 0, 0);

DateTime periodStartDate = new DateTime(2025, 2, 1, 0, 0, 0);
DateTime periodEndDate = new DateTime(2025, 2, 28, 0, 0, 0);

DateTime startDate = leaveStartDate.Date > periodStartDate ? leaveStartDate.Date : periodStartDate;
DateTime endDate = leaveEndDate.Date < periodEndDate ? leaveEndDate.Date : periodEndDate;

DateTime currentDate = startDate;
int totalLeaveDays = 0;

// To not include endDate as a leave day, use:
// currentDate < endDate)
while (currentDate <= endDate)
{
    if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday)
    {
        totalLeaveDays++;
    }
    currentDate = currentDate.AddDays(1);
};

Console.WriteLine(totalLeaveDays.ToString());
// 3
// 10
// 12
Sign up to request clarification or add additional context in comments.

Comments

0

Use the Subtraction method of the DateTime class for computing the number days for your time interval. Divide the number of days by 7 and multiply the result by 2. That gives you the number of leave days of the full 7-day weeks. Divide number of days modulo 7 ( % 7 instead of / 7). This gives you the remaining days which range from 0 through 6. Count the number of leave days contained in this result which is a number in the range from 0 through 2. Add this number to the number of leave days of the complete weeks and you are done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.