[英]How to exclude blackout days from a specific duration between two dates chosen from a two Datepicker?
I have two Datepicker and my object saves the duration between them with an excluding to the blackout days that exist between the two selected days.我有两个Datepicker ,我的 object 保存了它们之间的持续时间,并排除了两个选定日期之间存在的停电日。 (every Datepicker has blackout days)
(每个 Datepicker 都有停电日)
foreach (Model item in listmodels)
{
DateFrom.BlackoutDates.Add(new CalendarDateRange((item.startDate), (item.endDate)));
}
start_date = DateFrom.SelectedDate.Value;
end_date = DateTo.SelectedDate.Value;
var enddate = end_date.Date;
var startdate = start_date.Date;
leave_days = Convert.ToInt32(enddate.Subtract(startdate).TotalDays);
I suppose that the date range (Timespan duration) will be between 3/3/2021
and 3/27/2021
,我想日期范围(时间跨度持续时间)将在
3/3/2021
和3/27/2021
之间,
What I get is: 24 days我得到的是: 24天
The duration between these two dates should be ( expected result ) if I exclude Sunday
and Saturday
( weekend) and the blackout days that are presented as holidays: 17 days如果我排除
Sunday
和Saturday
(周末)以及作为假期呈现的停电日,这两个日期之间的持续时间应该是(预期结果): 17 天
How can I do that?我怎样才能做到这一点?
You can do your calc with code like this您可以使用这样的代码进行计算
DateTime start = new DateTime(2021,3,3);
DateTime end = new DateTime(2021,3,27);
// if you want to include the 27 then add + 1 after TotalDays
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
.Select(x => start.AddDays(x))
.Where(z => z.DayOfWeek != DayOfWeek.Saturday && z.DayOfWeek != DayOfWeek.Sunday)
.Count();
If you have a custom list of days that you have applied to the control as BlackoutDates then you can use that collection to exclude them from the count如果您有作为BlackoutDates应用于控件的自定义天数列表,那么您可以使用该集合将它们从计数中排除
DateTime start = new DateTime(2021,3,3);
DateTime end = new DateTime(2021,3,27);
List<DateTime> blackOut = new List<DateTime>
{
new DateTime(2021,3,10),
new DateTime(2021,3,11)
};
var days = Enumerable.Range(0, (int)(end - start).TotalDays)
.Select(x => start.AddDays(x))
.Where(z => z.DayOfWeek != DayOfWeek.Saturday &&
z.DayOfWeek != DayOfWeek.Sunday &&
!blackOut.Contains(z))
.Count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.