Get Number Of Working Day - CSharp System

CSharp examples for System:DateTime Day

Description

Get Number Of Working Day

Demo Code


using System.Threading;
using System.Globalization;
using System.Collections.Generic;
using System;//from w  w w.  j  av a  2  s. c om

public class Main{
        #endregion Month



        #region Number of working days per month
      
        public static int GetNumberOfWorkingDay(DateTime dateTime)
        {
            var total = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
            var firstDay = dateTime.GetFirstDay();
            var lastDay = dateTime.GetLastDay();
            var d1 = (6 + (int)firstDay.DayOfWeek) % 7;
            var d2 = (6 + (int)lastDay.DayOfWeek) % 7;

            var w1 = total + d1 + (6 - d2);
            var weeks = w1 / 7;
            var d4 = 4 - d2;
            var wd = w1 - weeks * 2 - (d1 < 6 ? d1 : 5) - (d4 > 0 ? d4 : 0);
            return wd;
        }
        /// <summary>
        /// Method for getting last day in month
        /// </summary>
        /// <param name="date">Date</param>
        /// <returns>Last day in month</returns>
        public static DateTime GetLastDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
        }
        /// <summary>
        /// Method for getting first day in month
        /// </summary>
        /// <param name="date">Date</param>
        /// <returns>First day in month</returns>
        public static DateTime GetFirstDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }
}

Related Tutorials