每月最后一个工作日(不是假期)的第x个月

时间:2015-08-13 14:21:12

标签: c# datetime

我怎样才能获得一个月的第4个工作日(这不是假期)? 八月二十六日(2015年)(星期三) 9月25日至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; }

        } 

任何想法?

1 个答案:

答案 0 :(得分:0)

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

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;
        }