C#: Get Business Working Day

Posted by Unknown On Friday, June 9, 2017 0 comments

First Method


        public static int GetWorkingDays(DateTime from, DateTime to)
        {
            var workingDay = 1 + (to - from).Days;
            return Enumerable
                .Range(0, workingDay)
                .Select(x => from.AddDays(x))
                .Count(x => x.DayOfWeek != DayOfWeek.Saturday && x.DayOfWeek != DayOfWeek.Sunday);
        }

Second Method


        public static double GetWorkingDays(DateTime startD, DateTime endD)
        {
            double workingDay = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;

            if (endD.DayOfWeek == DayOfWeek.Saturday) workingDay--;
            if (startD.DayOfWeek == DayOfWeek.Sunday) workingDay--;

            return workingDay;
        }

0 comments:

Post a Comment