以编程方式在asp.net日历中选择工作日

Select week days in asp.net Calendar programatically
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我正在尝试使用下面的代码来获取所有工作日.

    private List<DateTime> getWeekDays(DateTime dt)
    {
        List<DateTime> result = new List<DateTime>();

        int month = dt.Month;
        dt = dt.AddDays(-dt.Day + 1);//Sets dt to first day of month

        //Sets dt to the first week day of the month;
        if (dt.DayOfWeek != DayOfWeek.Monday)
            while (dt.DayOfWeek != DayOfWeek.Friday)
                dt = dt.AddDays(1);

        //Adds the week day and stops when next month is reached.
        while (dt.Month == month)
        {
            result.Add(dt);
            dt = dt.AddDays(dt.DayOfWeek == DayOfWeek.Friday ? 3 : 1);
        }
        return result;
    }

但这不能正常工作.当我将July月份传递给getWeekDays(DateTime dt)方法时,这将正确地赋予所有工作日.但是,如果我通过AugustSemptember,则上面的代码不会返回给定月份的所有工作日.

错误在哪里?

解决过程1
        private List<DateTime> getWeekDays(DateTime dt)
        {
            List<DateTime> result = new List<DateTime>();

            int month = dt.Month;
            dt = dt.AddDays(-dt.Day + 1);//Sets dt to first day of month

            //If dt is a weekend add days to move it to Monday (your while loop below will handle the rest)
            if(dt.DayOfWeek == DayOfWeek.Saturday)
            {
                dt = dt.AddDays(2);
            }
            else if (dt.DayOfWeek == DayOfWeek.Sunday)
            {
                dt = dt.AddDays(1);
            }

            //Adds the week day and stops when next month is reached.
            while (dt.Month == month)
            {
                result.Add(dt);
                dt = dt.AddDays(dt.DayOfWeek == DayOfWeek.Friday ? 3 : 1);
            }

            return result;
        }

I'm trying to get all week days using below code.

    private List<DateTime> getWeekDays(DateTime dt)
    {
        List<DateTime> result = new List<DateTime>();

        int month = dt.Month;
        dt = dt.AddDays(-dt.Day + 1);//Sets dt to first day of month

        //Sets dt to the first week day of the month;
        if (dt.DayOfWeek != DayOfWeek.Monday)
            while (dt.DayOfWeek != DayOfWeek.Friday)
                dt = dt.AddDays(1);

        //Adds the week day and stops when next month is reached.
        while (dt.Month == month)
        {
            result.Add(dt);
            dt = dt.AddDays(dt.DayOfWeek == DayOfWeek.Friday ? 3 : 1);
        }
        return result;
    }

But this is not working correctly. when i pass July month to getWeekDays(DateTime dt) method, this give all week days correctly. but if i pass August or Semptember, above code not return all week days of the given month.

Where is the error ?

Solutions1
        private List<DateTime> getWeekDays(DateTime dt)
        {
            List<DateTime> result = new List<DateTime>();

            int month = dt.Month;
            dt = dt.AddDays(-dt.Day + 1);//Sets dt to first day of month

            //If dt is a weekend add days to move it to Monday (your while loop below will handle the rest)
            if(dt.DayOfWeek == DayOfWeek.Saturday)
            {
                dt = dt.AddDays(2);
            }
            else if (dt.DayOfWeek == DayOfWeek.Sunday)
            {
                dt = dt.AddDays(1);
            }

            //Adds the week day and stops when next month is reached.
            while (dt.Month == month)
            {
                result.Add(dt);
                dt = dt.AddDays(dt.DayOfWeek == DayOfWeek.Friday ? 3 : 1);
            }

            return result;
        }
转载于:https://stackoverflow.com/questions/17397397/select-week-days-in-asp-net-calendar-programatically

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入