我怎样才能得到一个月的最后一个工作日(不是假期)的第 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/