0
\$\begingroup\$

I am fairly new into asp.net MVC c# I am working on request holiday app where an employee can request holidays. The problem is the app works fine, however, I am trying to set 8 hours a day when an employee requests a holiday I have scratching my head from 2 days already seen every topic in StackOverflow and here and still can't get to work if anyone has any suggestions it will be much appreciated. Here is a snippet of the calculation of the working days excluding weekends. Thank you in advance.

 public static class DateHelpers
    {
        public static int DaysBetweenExcludingWeekends(DateTime startDate, DateTime endDate)
        {

            var days = 0;
            while (startDate <= endDate)
            {
                if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday)

                {
                        days++;

                }

                startDate = startDate.AddDays(1);
            }

            return days;
        }

    }
\$\endgroup\$
  • \$\begingroup\$ Hello beetle_juice. Sadly CodeReview is for working code. Since your code has a problem, it's off-topic.. When your code works, please come back and we'll help you make it better :) \$\endgroup\$ – IEatBagels Nov 29 '19 at 14:27
  • \$\begingroup\$ Hi IEatBagels, my code works perfect, probably its badly asked question as I apologize for that, I just wanted to set the working hours to8 hours a day as at the moment when you request holiday by default are 24h.Also, I have done it somehow here is the snippet if (model.FromDate == model.ToDate) { model.FromDate = model.FromDate.AddHours(8); model.ToDate = model.ToDate.AddHours(16); } \$\endgroup\$ – beetle_juice Nov 29 '19 at 14:35
  • \$\begingroup\$ The problem is its working only when you request 1 day If request more than 1 day it's calculating 24hour day :) \$\endgroup\$ – beetle_juice Nov 29 '19 at 14:36
0
\$\begingroup\$

You can throw an error if someone is trying to set a work day to a day that should be a day off. Then you just need to handle this error.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.