C# 获取x';当月最后一个工作日(非节假日)

C# 获取x';当月最后一个工作日(非节假日),c#,datetime,C#,Datetime,例如,我如何从一个月中获得第四个最后工作日(不是假日)? 2015年8月26日(星期三) 2015年9月25日(星期五) 我的实际代码如下(我从中得到灵感) 但我有一个递归,它也有语法错误 static bool getLastXthWorkingDay(int x) { var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday }; int mon

例如,我如何从一个月中获得第四个最后工作日(不是假日)? 2015年8月26日(星期三) 2015年9月25日(星期五)

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

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

        } 
静态bool getLastXthWorkingDay(int x)
{
var weekends=新的星期一[]{DayOfWeek.Saturday,DayOfWeek.Sunday};
int month=DateTime.Now.month;
int year=DateTime.Now.year;
IFormatProvider culture=新系统。全球化。文化信息(“cs CZ”,真);
var假日=新日期时间[]{
Convert.ToDateTime(String.Format(“1.1.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“6.4.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“1.5.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“8.5.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“5.7.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“6.7.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“28.9.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“28.10.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“17.11.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“24.12.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“25.12.{0}”,年份),区域性),
Convert.ToDateTime(String.Format(“26.12.{0}”,年份),区域性)
};
//获取给定月份的天数。
int daysInMonth=DateTime.daysInMonth(年,月);
//这里我们创建一个从1到daysInMonth的可枚举项,
//并询问我们创建的DateTime对象是否属于周末,
//如果没有,请将其添加到我们的IEnumerable天数集合中。
IEnumerable businessDaysInMonth=可枚举。范围(1,daysInMonth)
其中(d=>!weekends.Contains(新日期时间(年、月、d).DayOfWeek));
var lastXthWorkingDay=businessDaysInMonth.Skip(Math.Max(0,businessDaysInMonth.Count()-x));
//这段代码有语法错误“)预期”但我不知道在哪里?
//if holidays.Contains(Convert.ToDateTime(String.Format(“{0}、{1}、{2}”、lastXthWorkingDay、month、year)、区域性))
//{    
//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;
        }
static int GetLastXthWorkingDay(int年、int月、int x)
{
var weekends=新的星期一[]{DayOfWeek.Saturday,DayOfWeek.Sunday};
var假日=新日期时间[]{
新日期时间(年,4,6),
新日期时间(年,5,1),
新日期时间(年,5,8),
新的日期时间(年,7,5),
新日期时间(第7年、第6年),
新日期时间(年,9,28),
新日期时间(年份,10,28),
新日期时间(年份,11,17),
新日期时间(年,12,24),
新日期时间(年,12,25),
新日期时间(年,12,26),
};
//获取给定月份的天数。
int daysInMonth=DateTime.daysInMonth(年,月);
//这里我们创建一个从1到daysInMonth的可枚举项,
//并询问我们创建的DateTime对象是否属于周末,
//如果没有,请将其添加到我们的IEnumerable天数集合中。
IEnumerable businessDaysInMonth=可枚举。范围(1,daysInMonth)
其中(d=>!weekends.Contains(新日期时间(年、月、d).DayOfWeek));
var lastXthWorkingDay=businessDaysInMonth.Skip(Math.Max(0,businessDaysInMonth.Count()-x));
foreach(最后一个工作日的var日)
{
if(假日.包含(新日期时间(年、月、日)))
{
返回GetLastXthWorkingDay(年、月、x+1);
//第1天返回;
}
else{返回日;}
}
返回0;
}

语法错误的确切含义是什么?同样对于假日,您指的是周末,对吗?您不应该解析字符串,而应该使用
new DateTime()
构造函数。错误是“'),应该出现在假日行上。包含条件您注释掉的代码调用
isTodayLastXthWorkingDay
,我们不知道这是什么,因此,除非它调用
getLastXthWorkingDay
,否则没有递归。有没有方法让函数返回
false
?从我看来,这不是真的,就是。。。