首页  编辑  

计算两个时间中出现了多少个工作日

Tags: /C#/日期时间/   Date Created:

public int TotalWorkday( DateTime AStart, DateTime AEnd)

{

    TimeSpan vTimeSpan = new TimeSpan (AEnd . Ticks - AStart . Ticks);

    //

    int Result = (( int )vTimeSpan . TotalDays / 7 ) * 5 ;

    //

    for ( int i = 0 ; i <= vTimeSpan . TotalDays % 7 ; i ++ )

        switch (AStart . AddDays(i) . DayOfWeek)

       {

            case DayOfWeek . Saturday:

            case DayOfWeek . Sunday:

                break ;

            default :

               Result ++ ;

                break ;

       }

    return Result;

} /* TotalWorkday */

private void button1_Click( object sender , EventArgs e)

{

   Text = TotalWorkday( new DateTime ( 2007 , 4 , 6 ),

        new DateTime ( 2007 , 4 , 11 )) . ToString();

}