c# Web 应用程序如何获得不包括周末和公共(public)假期的工作日

标签 c# asp.net

背景资料

刚开始学习 C#,我正在尝试构建一个简单的 Web 应用程序来计算 2 个日期之间的工作日数。

The UI of the web app

enter image description here

基本逻辑是当用户输入日期(即 01/05/2018)并点击按钮时,它会计算工作日总数(不包括周末和公共(public)假期)。

现在的问题是计算不准确,即在 23/05/2018 和 31/05/2018 之间显示 6,应该是 7 天。并且在计算过程中不考虑日期

namespace testtest
{
public partial class First : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }


    //on btn click
    protected void Button1_Click(object sender, EventArgs e)
    {


        string dt = TextBox1.Text;
        DateTime dtDDMMYYYY = ParseDate(dt);


        string dt2 = TextBox2.Text;
        DateTime dtDDMMYYYY2 = ParseDate(dt2);

       List<DateTime> list = new List<DateTime> { 
        DateTime.ParseExact("04/05/2018", "dd/MM/yyyy", 
       CultureInfo.InvariantCulture) };

        DaysLeft(dtDDMMYYYY, dtDDMMYYYY2, true, list);
    }


    public DateTime ParseDate(string date)
    {
        DateTimeFormatInfo dateFormatProvider = new DateTimeFormatInfo();
        dateFormatProvider.ShortDatePattern = "dd/MM/yyyy";
        return DateTime.Parse(date, dateFormatProvider);
    }



    public int DaysLeft(DateTime startDate, DateTime endDate, Boolean 
     excludeWeekends, List<DateTime> excludeDates)
    {


        int count = 0;
        for (DateTime index = startDate; index < endDate; index = 
        index.AddDays(1))
        {
            if (excludeWeekends && index.DayOfWeek != DayOfWeek.Sunday && 
           index.DayOfWeek != DayOfWeek.Saturday)
            {
                bool excluded = false; ;
                for (int i = 0; i < excludeDates.Count; i++)
                {
                    if (index.Date.CompareTo(excludeDates[i].Date) == 0)
                    {
                        excluded = true;
                        break;
                    }
                }

                if (!excluded)
                {
                    count++;
                }
            }
        }
        result.Text = count.ToString();
        return count;
    }


}

最佳答案

保持简单

public int DaysLeft(DateTime startDate, DateTime endDate, Boolean excludeWeekends, List<DateTime> excludeDates) {
    int count = 0;
    for (DateTime index = startDate; index <= endDate; index = index.AddDays(1)) {
        if (excludeWeekends && (index.DayOfWeek == DayOfWeek.Sunday || index.DayOfWeek == DayOfWeek.Saturday))
            continue;

        if (excludeDates.Contains(index.Date))
            continue;

        count++;
    }
    return count;
}

如果日期是周末并且标记了 excludeWeekends,则继续到下一个日期,如果日期包含在 excludeDates 中,则继续,否则计算当天。

关于c# Web 应用程序如何获得不包括周末和公共(public)假期的工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50314918/

相关文章:

c# - 如何从代码隐藏中获取 javascript 中设置的值?

c# - Xamarin/Mono 找不到 System.Core

c# - 如何在 mvc c# 中用图像替换文本单元格

asp.net - 母版页 Asp.net 上的回发事件之前的验证

c# - 在 C# Web 应用程序中插入查询超时,从 SQL Server Management Studio 运行正常

asp.net - 对一个页面执行一次脚本

c# - ASP.NET中如何在不手动刷新的情况下更新GridView中的数据?

c# - C# 中的工厂、构造函数和泛型

c# - 从 Windows 8 应用程序在 Windows 资源管理器中打开文件?

c# - 打印 <div> 标签内的图表