在做项目的时候要求得到该月的有效工作日,于是开始写静态方法,后来由于还要获取当月开始到现在的有效工作日,于是再写方法,最后因为以后的项目还要用于是继续扩展,直接封装出一个类来。
由于时间问题,静态方法整理可能不是很好,希望大家提意见,以便更改。
源码下载:https://files.cnblogs.com/tishifu/WorkDateProvider.rar
/*************************************************
*
* Creator: Xingmai
* Date : 2007-07-16
*
* Edit :
* Date :
* Content: cs
*
* ***********************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace Xingmai.Support
{
/// <summary>
/// 获取有效工作日
/// </summary>
public class WorkDateProvider
{
#region 静态方法段
/// <summary>
/// 在一段内获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <param name="intNoContain">不包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateBy2Date(DateTime dt1, DateTime dt2, int intNoContain)
{
//判断不包含天数是否合法
if (intNoContain <= 0)
throw (new Exception("不包含天数必须大于0"));
return GetWorkDateBy2Date(dt1, dt2) - intNoContain;
}
/// <summary>
/// 在一段内获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <param name="intNoContain">不包含日期数</param>
/// <param name="intContain">包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateBy2Date(DateTime dt1, DateTime dt2, int intNoContain, int intContain)
{
if (intContain <= 0)
throw (new Exception("包含天数必须大于0"));
return GetWorkDateBy2Date(dt1, dt2, intNoContain) + intContain;
}
/// <summary>
/// 在一段内获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <param name="strarrContain">包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateBy2Date(DateTime dt1, DateTime dt2, string[] strarrNoContain, string[] strarrContain)
{
int intContain = 0;
foreach (string strContain in strarrContain)
{
try
{
DateTime dtDuring = DateTime.Parse(strContain);
if (dt1 <= dtDuring && dtDuring <= dt2)
switch (dtDuring.DayOfWeek) //判断是否本来不是工作日的日期
{
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
intContain++;
break;
default:
break;
}
}
catch (Exception ex)
{
throw (ex);
}
}
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain) + intContain;
}
/// <summary>
/// 在一段内获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateBy2Date(DateTime dt1, DateTime dt2, string[] strarrNoContain)
{
int intNotContain = 0;
foreach (string strNoContain in strarrNoContain)
{
try
{
DateTime dtDuring = DateTime.Parse(strNoContain);
if (dt1 <= dtDuring && dtDuring <= dt2)
{
switch (dtDuring.DayOfWeek)//判断原来是工作日
{
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
intNotContain++;
break;
default:
break;
}
}
}
catch (Exception ex)
{
throw (ex);
}
}
return GetWorkDateBy2Date(dt1, dt2, intNotContain);
}
/// <summary>
/// 在一段内获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateBy2Date(DateTime dt1, DateTime dt2)
{
if (dt1 > dt2)
throw (new Exception("开始日期必须大于结束日期"));
int intTotalDays = 0;
for (DateTime dtDuring = dt1; dtDuring <= dt2; dtDuring = dtDuring.AddDays(1))
{
switch (dtDuring.DayOfWeek)//判断是否为工作日
{
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
intTotalDays++;
break;
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
break;
}
}
return intTotalDays;
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthAndYear(int intMonth, int intYear)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", intYear, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", intYear, intMonth, System.DateTime.DaysInMonth(intYear, intMonth)));
return GetWorkDateBy2Date(dt1, dt2);
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
/// <param name="intNoContain">不包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthAndYear(int intMonth, int intYear, int intNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", intYear, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", intYear, intMonth, System.DateTime.DaysInMonth(intYear, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain);
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
/// <param name="intNoContain">不包含日期数</param>
/// <param name="intContain">包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthAndYear(int intMonth, int intYear, int intNoContain, int intContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", intYear, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", intYear, intMonth, System.DateTime.DaysInMonth(intYear, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain, intContain);
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthAndYear(int intMonth, int intYear, string[] strarrNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", intYear, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", intYear, intMonth, System.DateTime.DaysInMonth(intYear, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain);
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <param name="strarrContain">包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthAndYear(int intMonth, int intYear, string[] strarrNoContain, string[] strarrContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", intYear, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", intYear, intMonth, System.DateTime.DaysInMonth(intYear, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain, strarrContain);
}
/// <summary>
/// 获取本年制定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthThisYear(int intMonth)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, intMonth, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, intMonth)));
return GetWorkDateBy2Date(dt1, dt2);
}
/// <summary>
/// 获取本年制定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intNoContain">不包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthThisYear(int intMonth, int intNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, intMonth, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain);
}
/// <summary>
/// 获取本年制定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intNoContain">不包含日期数</param>
/// <param name="intContain">包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthThisYear(int intMonth, int intNoContain, int intContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, intMonth, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain, intContain);
}
/// <summary>
/// 获取本年制定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthThisYear(int intMonth, string[] strarrNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, intMonth, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain);
}
/// <summary>
/// 获取本年制定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <param name="strarrContain">包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByMonthThisYear(int intMonth, string[] strarrNoContain, string[] strarrContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, intMonth));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, intMonth, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, intMonth)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain, strarrContain);
}
/// <summary>
/// 获取本年本月的有效工作日
/// </summary>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByThisMonthThisYear()
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, DateTime.Now.Date.Month));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month)));
return GetWorkDateBy2Date(dt1, dt2);
}
/// <summary>
/// 获取本年本月的有效工作日
/// </summary>
/// <param name="intNoContain">不包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByThisMonthThisYear(int intNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, DateTime.Now.Date.Month));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain);
}
/// <summary>
/// 获取本年本月的有效工作日
/// </summary>
/// <param name="intNoContain">不包含日期数</param>
/// <param name="intContain">包含日期数</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByThisMonthThisYear(int intNoContain, int intContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, DateTime.Now.Date.Month));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month)));
return GetWorkDateBy2Date(dt1, dt2, intNoContain, intContain);
}
/// <summary>
/// 获取本年本月的有效工作日
/// </summary>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByThisMonthThisYear(string[] strarrNoContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, DateTime.Now.Date.Month));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain);
}
/// <summary>
/// 获取本年本月的有效工作日
/// </summary>
/// <param name="strarrNoContain">不包含日期列表</param>
/// <param name="strarrContain">包含日期列表</param>
/// <returns>有效工作日数</returns>
public static int GetWorkDateByThisMonthThisYear(string[] strarrNoContain, string[] strarrContain)
{
DateTime dt1 = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Date.Year, DateTime.Now.Date.Month));
DateTime dt2 = DateTime.Parse(string.Format("{0}-{1}-{2}", DateTime.Now.Date.Year, DateTime.Now.Date.Month, System.DateTime.DaysInMonth(DateTime.Now.Date.Year, DateTime.Now.Date.Month)));
return GetWorkDateBy2Date(dt1, dt2, strarrNoContain, strarrContain);
}
#endregion
#region 内部变量
protected int _WorkDateYear = DateTime.Now.Year;
protected int _WorkDateMonth = DateTime.Now.Month;
protected string[] _NoContainDays = null;
protected string[] _ContainDays = null;
protected DateTime _WorkDateStart = DateTime.Now;
protected DateTime _WorkDateEnd = DateTime.Now;
protected GetDaysType _GetDaysType = GetDaysType.ByMonthAndYear;
#endregion
#region 属性
/// <summary>
/// 制定年份
/// </summary>
public int WorkDateYear
{
set
{
_WorkDateYear = value;
}
get
{
return _WorkDateYear;
}
}
/// <summary>
/// 制定月份
/// </summary>
public int WorkDateMonth
{
set
{
_WorkDateMonth = value;
}
get
{
return _WorkDateMonth;
}
}
/// <summary>
/// 不包含日期
/// </summary>
public string[] NoContainDays
{
set
{
_NoContainDays = value;
}
get
{
return _NoContainDays;
}
}
/// <summary>
/// 包含日期
/// </summary>
public string[] ContainDays
{
set
{
_ContainDays = value;
}
get
{
return _ContainDays;
}
}
/// <summary>
/// 开始日期
/// </summary>
public DateTime WorkDateStart
{
set
{
_WorkDateStart = value;
}
get
{
return _WorkDateStart;
}
}
/// <summary>
/// 结束日期
/// </summary>
public DateTime WorkDateEnd
{
set
{
_WorkDateEnd = value;
}
get
{
return _WorkDateEnd;
}
}
/// <summary>
/// 获取工作日类型
/// </summary>
public GetDaysType EmunGetDaysType
{
set
{
_GetDaysType = value;
}
get
{
return _GetDaysType;
}
}
#endregion
#region 调用枚举
/// <summary>
/// 获取工作日类型枚举
/// </summary>
public enum GetDaysType
{
ByDateStartAndEnd,
ByMonthAndYear
}
#endregion
#region 实例构造函数
/// <summary>
/// 默认本年本月获取有效工作日
/// </summary>
public WorkDateProvider()
{
EmunGetDaysType = GetDaysType.ByMonthAndYear;
}
/// <summary>
/// 设定开始日期和结束日期获取有效工作日
/// </summary>
/// <param name="dt1">开始日期</param>
/// <param name="dt2">结束日期</param>
public WorkDateProvider(DateTime dt1, DateTime dt2)
{
WorkDateStart = dt1;
WorkDateEnd = dt2;
EmunGetDaysType = GetDaysType.ByDateStartAndEnd;
}
/// <summary>
/// 设定本年指定月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
public WorkDateProvider(int intMonth)
{
WorkDateMonth = intMonth;
EmunGetDaysType = GetDaysType.ByMonthAndYear;
}
/// <summary>
/// 设定指定年月的有效工作日
/// </summary>
/// <param name="intMonth">月份</param>
/// <param name="intYear">年份</param>
public WorkDateProvider(int intMonth, int intYear)
{
WorkDateMonth = intMonth;
WorkDateYear = intYear;
EmunGetDaysType = GetDaysType.ByMonthAndYear;
}
#endregion
#region 实例方法段
/// <summary>
/// 获取有效时间
/// </summary>
/// <returns>有效工作日数</returns>
public int GetWorkDays()
{
return GetWorkDays(EmunGetDaysType);
}
/// <summary>
/// 获取有效时间
/// </summary>
/// <param name="getDaysType">获取时间类型</param>
/// <returns>有效工作日数</returns>
public int GetWorkDays(GetDaysType getDaysType)
{
if (getDaysType == GetDaysType.ByDateStartAndEnd)
return GetWorkDayByDateStartAndEnd();
else
return GetWorkDayByYearAndMonth();
}
/// <summary>
/// 获取制定年月的有效工作日
/// </summary>
/// <returns>有效工作日数</returns>
private int GetWorkDayByYearAndMonth()
{
WorkDateStart = DateTime.Parse(string.Format("{0}-{1}-01", WorkDateYear, WorkDateMonth));
WorkDateEnd = DateTime.Parse(string.Format("{0}-{1}-{2}", WorkDateYear, WorkDateMonth, DateTime.DaysInMonth(WorkDateYear, WorkDateMonth)));
return GetWorkDayByDateStartAndEnd();
}
/// <summary>
/// 获取开始日期到结束日期之间的有效工作日
/// </summary>
/// <returns>有效工作日数</returns>
protected int GetWorkDayByDateStartAndEnd()
{
//判断时间有效性
if (WorkDateStart > WorkDateEnd)
throw (new Exception("开始日期大于结束日期"));
int intNoContainDays = 0;
int intContainDays = 0;
int intTotalDays = 0;
//计算不包含日期数
if (NoContainDays != null)
foreach (string strNoContain in NoContainDays)
{
DateTime dtDuring = DateTime.Parse(strNoContain);
if (WorkDateStart <= dtDuring && dtDuring <= WorkDateEnd)
{
switch (dtDuring.DayOfWeek)
{
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
intNoContainDays++;
break;
default:
break;
}
}
}
//计算包含日期数
if (ContainDays != null)
foreach (string strContain in ContainDays)
{
DateTime dtDuring = DateTime.Parse(strContain);
if (WorkDateStart <= dtDuring && dtDuring <= WorkDateEnd)
{
switch (dtDuring.DayOfWeek)
{
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
intContainDays++;
break;
default:
break;
}
}
}
//计算常规日期数
for (DateTime dtDuring = WorkDateStart; dtDuring <= WorkDateEnd; dtDuring = dtDuring.AddDays(1))
{
switch (dtDuring.DayOfWeek)
{
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
intTotalDays++;
break;
default:
break;
}
}
return intTotalDays - intNoContainDays + intContainDays;
}
#endregion
}
}