如何从两个Datepicker中选择的两个日期之间的特定持续时间中排除停电天数? [英] How to exclude blackout days from a specific duration between two dates chosen from a two Datepicker?

查看:50
本文介绍了如何从两个Datepicker中选择的两个日期之间的特定持续时间中排除停电天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Datepicker ,并且我的对象保存了它们之间的持续时间,但排除了两个选定日期之间存在的停电天数.(每个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. (every Datepicker has blackout days)

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

我想日期范围(时间间隔的持续时间)应该在 3/3/2021 3/27/2021 之间,

I suppose that the date range (Timespan duration) will be between 3/3/2021 and 3/27/2021,

我得到的是: 24天

如果我排除 Sunday Saturday (周末)和表示假期的停电天数,则这两个日期之间的持续时间应为(预期结果): 17天

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

我该怎么做?

推荐答案

您可以使用以下代码进行计算

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

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();

这篇关于如何从两个Datepicker中选择的两个日期之间的特定持续时间中排除停电天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆