Calculate the number of business days between two dates?

后端 未结 30 1662
悲&欢浪女
悲&欢浪女 2020-11-22 14:54

In C#, how can I calculate the number of business (or weekdays) days between two dates?

30条回答
  •  孤城傲影
    2020-11-22 15:48

    Ok. I think it's time to post the right answer:

    public static double GetBusinessDays(DateTime startD, DateTime endD)
    {
        double calcBusinessDays =
            1 + ((endD - startD).TotalDays * 5 -
            (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;
    
        if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
        if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;
    
        return calcBusinessDays;
    }
    

    Original Source:

    http://alecpojidaev.wordpress.com/2009/10/29/work-days-calculation-with-c/

    P.S. Solutions posted above making me sic for some reason.

提交回复
热议问题