C# 获取没有周六和周日的任何月份的天数?
有一个输入字符串字段Januray 2020。我想计算给定月份和年份的总工作日数C# 获取没有周六和周日的任何月份的天数?,c#,C#,有一个输入字符串字段Januray 2020。我想计算给定月份和年份的总工作日数 我们可以用简单的c语言实现吗?因为您还没有定义工作日,所以可以使用此函数根据谓词迭代一个月内的所有工作日: IEnumerable<DateTime> WorkDaysInMonth(int year, int month, Func<DateTime,bool> isNonWorkDay) { var now = new DateTime(year, month, 1);
我们可以用简单的c语言实现吗?因为您还没有定义工作日,所以可以使用此函数根据谓词迭代一个月内的所有工作日:
IEnumerable<DateTime> WorkDaysInMonth(int year, int month, Func<DateTime,bool> isNonWorkDay)
{
var now = new DateTime(year, month, 1);
while(now.Month == month)
{
if(!isNonWorkDay(now))
{
yield return now;
}
now = now.AddDays(1);
}
}
同样,如果您有一些日历系统告诉您有关公共假日的信息,那么您可以在谓词中添加一个调用,以查看提供的日期是否为假日
var s = "January 2020";
var d = DateTime.Parse(s);
Console.WriteLine(d);
var x = DateTime.DaysInMonth(d.Year, d.Month);
Console.WriteLine(x);
var w = Enumerable.Range(1, x)
.Select( i => new DateTime( d.Year, d.Month, i))
.Count( i => i.DayOfWeek != DayOfWeek.Saturday && i.DayOfWeek != DayOfWeek.Sunday );
Console.WriteLine(w);
演示:公共假日或其他特殊日期也算工作日吗?您好,欢迎加入一些示例输入,相应的示例输出,并向我们展示一些您已经尝试过的代码!它回答了你的问题吗?这回答了你的问题吗?
var s = "January 2020";
var d = DateTime.Parse(s);
Console.WriteLine(d);
var x = DateTime.DaysInMonth(d.Year, d.Month);
Console.WriteLine(x);
var w = Enumerable.Range(1, x)
.Select( i => new DateTime( d.Year, d.Month, i))
.Count( i => i.DayOfWeek != DayOfWeek.Saturday && i.DayOfWeek != DayOfWeek.Sunday );
Console.WriteLine(w);