判断工作日的函数
工作日:除周六、周日
节假日:周六、周日、1月1-3、5月1-7、10月1-7
date(2005-10-20);public void date()
{
????
}

解决方案 »

  1.   

    public bool IsWeekday(DateTime TheDay)
    {
    if(TheDay.DayOfWeek==DayOfWeek.Saturday||TheDay.DayOfWeek==DayOfWeek.Sunday)
       return false;
    else
       return true;
    }
      

  2.   

    对于周末有一段规则的,使用DayOfWeek属性来进行判断。
    对于节假日是可配置的,可通过读取配置文件的方式得到其集合。再将日期的年、月进行比较可得到。
      

  3.   

    // 判断工作日的函数
    // 工作日:除周六、周日
    // 节假日:周六、周日、1月1-3、5月1-7、10月1-7
    public int GetDate( DateTime p_dTime)
    {
    int intYear=p_dTime.Year;
    int intMonth=p_dTime.Month;
    int intDays=p_dTime.Day;
    int intDaysInMonth = DateTime.DaysInMonth(intYear,intMonth );
    int intNum=0;
    switch(intMonth)
    {
    case 1: 
    if(intDays<=3)
    {
    intDaysInMonth = intDaysInMonth-3 ;
    p_dTime=p_dTime.AddDays(3-intDays);
    }
    else
    {
    intDaysInMonth = intDaysInMonth-intDays;
    }
    break;
    case 5:
    case 10:
    if(intDays<=7)
    {
    intDaysInMonth = intDaysInMonth-7 ;
    p_dTime=p_dTime.AddDays(7-intDays);
    }
    else
    {
    intDaysInMonth = intDaysInMonth-intDays;
    }
    break;
    default:
    break;
    }
    for(int i=0;i<intDaysInMonth;i++)
    {    
    p_dTime=p_dTime.AddDays(1);
    if(p_dTime.DayOfWeek==DayOfWeek.Saturday|| p_dTime.DayOfWeek==DayOfWeek.Sunday)
    {
    intNum++;
    }
    }
    return intDaysInMonth-intNum;
    }
      

  4.   

    create function WorkDays(@mStartDate datetime,@mEndDate datetime)
    returns integer
    begin
      declare @m integer
      declare @n integer
      if @mEndDate < @mStartDate set @n=-1 else
      begin
      set @m=datediff(dd,@mStartDate,@mEndDate)
      set @n=(@m/7)*2
      if  @m % 7<>0 
      begin
        if datepart(weekday,@mStartDate)>datepart(weekday,@mEndDate)
     set @n=@n+2
        if datepart(weekday,@mStartDate)=1 set @n=@n+1
        if datepart(weekday,@mEndDate)=1 set @n=@n-1
      end
      set @n=@m-@n
      end
      return(@n)
    end
    gocreate table tb_Holiday(
    HDate smalldatetime primary key clustered,-- 日期
      Type smallint,--增减
    Name nvarchar(50) not null) 
    goinsert into tb_Holiday
    select  '2005-1-1',0,'元旦' union -- 周六
    select  '2005-1-2',0,'元旦' union -- 周日
    select  '2005-1-3',-1,'元旦-' union
    select  '2005-2-5',1,'春节+' union
    select  '2005-2-6',1,'春节+' union
    select  '2005-2-9',-1,'春节-' union
    select  '2005-2-10',-1,'春节-' union
    select  '2005-2-11',-1,'春节-' union
    select  '2005-2-12',0,'春节' union -- 周六
    select  '2005-2-13',0,'春节' union -- 周日
    select  '2005-2-14',-1,'春节-' union
    select  '2005-2-15',-1,'春节-' select dbo.WorkDays('2004-12-20',getdate())+sum(Type) from tb_Holiday where HDate >'2004-12-20' 
    drop table tb_Holiday
    drop function WorkDays/*
                
    ----------- 
    67
    *//*
    双休日直接从函数取得,把节假日的排班信息记录在一个表中,
    排班信息包括上班的双休日(+1),节日中的休息日(如果是双休则type为0,不是双休则-1)
    这样辅助表中的记录应该很少,而且直观,最后把type求和一下就可以知道差值了
    */