C# 在C中计算日期范围之间的假期数#
我想知道如何计算C#中日期范围之间的假期数 示例:日期范围为2012年1月1日至2012年2月1日C# 在C中计算日期范围之间的假期数#,c#,C#,我想知道如何计算C#中日期范围之间的假期数 示例:日期范围为2012年1月1日至2012年2月1日 conditions: holiday1=>From Date (30th Dec 2011 to 2nd jan 2012) holiday2 =>From Date(16th jan 2012 to 18 th jan 2012) holiday3=>From Date(30the jan 2012 to 2nd Feb 2012) 由于第一个假期
conditions:
holiday1=>From Date (30th Dec 2011 to 2nd jan 2012)
holiday2 =>From Date(16th jan 2012 to 18 th jan 2012)
holiday3=>From Date(30the jan 2012 to 2nd Feb 2012)
由于第一个假期到1月2日为止,我需要计算假期1的2天以及假期3的2天和假期2,这对我来说不是一个计算问题。首先,你需要一个单一的、一致的假期定义。有很多。有美国联邦政府的定义,有纽约证券交易所的定义,甚至有些州也有自己的节日(例如马萨诸塞州的爱国者节)。当然,我忽略了美国不是世界上唯一的国家这一事实。那么,假设您已经制定了这个定义,并将其编入了一个方法:
bool IsHoliday(this DateTime date) { // }
现在你可以说:
int CountHolidaysBetween(DateTime start, DateTime end) {
int days = end.Day.Substract(start.Day).Days;
return Enumerable.Range(0, days + 1)
.Select(day => start.Day.AddDays(day))
.Count(d => d.IsHoliday());
}
或者,如果你觉得这太花哨了:
int CountHolidaysBetween(DateTime start, DateTime end) {
DateTime current = start.Day;
int count = 0;
while(current <= end.Day) {
if(current.IsHoliday()) { count++; }
current = current.AddDays(1);
}
return count;
}
int CountHolidaysBetween(日期时间开始,日期时间结束){
当前日期时间=开始日期;
整数计数=0;
然而(当前首先,你需要一个统一的假日定义。有很多。有美国联邦政府的定义,有纽约证券交易所的定义,甚至有些州也有自己的假日(例如马萨诸塞州的爱国者节).当然,我忽略了一个事实,美国不是世界上唯一的国家。所以,假设你已经制定了这个定义,并将其编入了一种方法:
bool IsHoliday(this DateTime date) { // }
现在你可以说:
int CountHolidaysBetween(DateTime start, DateTime end) {
int days = end.Day.Substract(start.Day).Days;
return Enumerable.Range(0, days + 1)
.Select(day => start.Day.AddDays(day))
.Count(d => d.IsHoliday());
}
或者,如果你觉得这太花哨了:
int CountHolidaysBetween(DateTime start, DateTime end) {
DateTime current = start.Day;
int count = 0;
while(current <= end.Day) {
if(current.IsHoliday()) { count++; }
current = current.AddDays(1);
}
return count;
}
int CountHolidaysBetween(日期时间开始,日期时间结束){
当前日期时间=开始日期;
整数计数=0;
while(当前)