c# - 获取一个月中的第 x 个最后一个工作日(不是假期)

标签 c# datetime

我怎样才能得到一个月的最后一个工作日(不是假期)的第 4 个? 八月 26.8.2015 日(星期三) 9月25.9.2015日(星期五)

我的实际代码如下(我从 here 获得灵感) 但我有一个递归,它也有语法错误。

 static bool getLastXthWorkingDay(int x)
        {
            var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
            int month = DateTime.Now.Month;
            int year = DateTime.Now.Year;

            IFormatProvider culture = new System.Globalization.CultureInfo("cs-CZ", true);
            var holidays = new DateTime[] { 
                Convert.ToDateTime(String.Format("1.1.{0}",year),culture), 
                Convert.ToDateTime(String.Format("6.4.{0}",year),culture),
                Convert.ToDateTime(String.Format("1.5.{0}",year),culture), 
                Convert.ToDateTime(String.Format("8.5.{0}",year),culture), 
                Convert.ToDateTime(String.Format("5.7.{0}",year),culture), 
                Convert.ToDateTime(String.Format("6.7.{0}",year),culture), 
                Convert.ToDateTime(String.Format("28.9.{0}",year),culture), 
                Convert.ToDateTime(String.Format("28.10.{0}",year),culture), 
                Convert.ToDateTime(String.Format("17.11.{0}",year),culture),
                Convert.ToDateTime(String.Format("24.12.{0}",year),culture), 
                Convert.ToDateTime(String.Format("25.12.{0}",year),culture), 
                Convert.ToDateTime(String.Format("26.12.{0}",year),culture)
            };

            //Fetch the amount of days in your given month.
            int daysInMonth = DateTime.DaysInMonth(year, month);

            //Here we create an enumerable from 1 to daysInMonth,
            //and ask whether the DateTime object we create belongs to a weekend day,
            //if it doesn't, add it to our IEnumerable<int> collection of days.
            IEnumerable<int> businessDaysInMonth = Enumerable.Range(1, daysInMonth)
                                                   .Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek));

            var lastXthWorkingDay = businessDaysInMonth.Skip(Math.Max(0, businessDaysInMonth.Count() - x));

            // This code has syntax error ") expected" but i dont see where ?
            //if holidays.Contains(Convert.ToDateTime(String.Format("{0},{1},{2}", lastXthWorkingDay, month, year),culture))
            //{    
            //    getLastXthWorkingDay(x-1);
            //} 
            //else { return true; }

        } 

有什么想法吗?

最佳答案

好的,我想我明白了。谢谢大家

static int GetLastXthWorkingDay(int year, int month, int x)
        {
            var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };

            var holidays = new DateTime[] { 
                new DateTime(year, 4, 6),
                new DateTime(year, 5, 1), 
                new DateTime(year, 5, 8), 
                new DateTime(year, 7, 5), 
                new DateTime(year, 7, 6), 
                new DateTime(year, 9, 28), 
                new DateTime(year, 10, 28), 
                new DateTime(year, 11, 17), 
                new DateTime(year, 12, 24), 
                new DateTime(year, 12, 25), 
                new DateTime(year, 12, 26), 
            };

            //Fetch the amount of days in your given month.
            int daysInMonth = DateTime.DaysInMonth(year, month);

            //Here we create an enumerable from 1 to daysInMonth,
            //and ask whether the DateTime object we create belongs to a weekend day,
            //if it doesn't, add it to our IEnumerable<int> collection of days.
            IEnumerable<int> businessDaysInMonth = Enumerable.Range(1, daysInMonth)
                                                   .Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek));

            var lastXthWorkingDay = businessDaysInMonth.Skip(Math.Max(0, businessDaysInMonth.Count() - x));

                foreach (var day in lastXthWorkingDay)
                {
                    if ( holidays.Contains(new DateTime(year, month, day)) )
                    {
                        return GetLastXthWorkingDay(year, month, x + 1);
                        //return day-1;
                    }
                    else { return day; }
                }

                return 0;
        }

关于c# - 获取一个月中的第 x 个最后一个工作日(不是假期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990879/

相关文章:

python - 如何以常规范式打印日期?

python - Pandas 循环自定义日期(月年)到(月年+N)进行绘图

sql - 在 Microsoft SQL Server 中比较日期的最佳方法是什么?

C# wininet InternetSetOption 选项

c# - JSON writer - 替换 xmlwriter?

C# 将周作为 DateTime 返回

c# - Entity Framework 日期时间和 UTC

c# - 如何使用属性名称中的空格反序列化 JSON?

c# - 基于 Kinect 数据的 HMM 3D 手势识别特征提取

c# - 如何从 `BindingExpression` 对象获取 `Binding`?