C# 如何通过同时考虑时间来计算两个给定日期之间的周末(以分钟为单位)

C# 如何通过同时考虑时间来计算两个给定日期之间的周末(以分钟为单位),c#,C#,我有两个字段startdate和enddate,在这两个字段中,我需要计算这两个日期之间的周末数,并以分钟为单位显示它们。例如,开始日期为2019年11月1日00:00:00,结束日期为2019年11月3日12:00:00,我应该在给定日期之间的星期六和部分星期日(1.5天周末)中获得输出 我尝试了下面的代码,该代码不计算给定场景下的周末时间 public int CountOfWeekEnds(DateTime startDate, DateTime endDate) {

我有两个字段startdate和enddate,在这两个字段中,我需要计算这两个日期之间的周末数,并以分钟为单位显示它们。例如,开始日期为2019年11月1日00:00:00,结束日期为2019年11月3日12:00:00,我应该在给定日期之间的星期六和部分星期日(1.5天周末)中获得输出

我尝试了下面的代码,该代码不计算给定场景下的周末时间

public int CountOfWeekEnds(DateTime startDate, DateTime endDate)
    {
        int weekEndCount = 0;
        if (startDate > endDate)
        {
            DateTime temp = startDate;
            startDate = endDate;
            endDate = temp;
        }
        TimeSpan diff = endDate - startDate;
        int days = diff.Days;
        for (var i = 0; i <= days; i++)
        {
            var testDate = startDate.AddDays(i);
            if (testDate.DayOfWeek == DayOfWeek.Saturday || testDate.DayOfWeek == DayOfWeek.Sunday)
            {
                if (testDate.Minute > 0)
                {
                    weekEndCount += 1;
                }
            }
        }
        return weekEndCount;
    }
public int CountOfWeekEnds(日期时间开始日期、日期时间结束日期)
{
int weekEndCount=0;
如果(开始日期>结束日期)
{
日期时间温度=开始日期;
开始日期=结束日期;
结束日期=温度;
}
TimeSpan diff=结束日期-开始日期;
整数天=差异天;
对于(变量i=0;i 0)
{
weekEndCount+=1;
}
}
}
返回周数;
}

将输出显示为2天的周末,而不是日期之间的1.5天。如果我理解正确,请建议我如何做到这一点。你所说的
周末
是指
周六
周日

我使用此代码计算两个日期之间存在多少
DayOfWeek

public static int CountOfWeekEnds(DateTime start, DateTime end) {
    return CountDays(DayOfWeek.Saturday, start, end) + CountDays(DayOfWeek.Sunday, start, end);
}

public static int CountDays(DayOfWeek day, DateTime start, DateTime end)
{
    TimeSpan ts = end - start;                       // Total duration
    int count = (int)Math.Floor(ts.TotalDays / 7);   // Number of whole weeks
    int remainder = (int)(ts.TotalDays % 7);         // Number of remaining days
    int sinceLastDay = end.DayOfWeek - day;          // Number of days since last [day]
    if (sinceLastDay < 0) sinceLastDay += 7;         // Adjust for negative days since last [day]

    // If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
    if (remainder >= sinceLastDay) count++;          

    return count;
}
publicstaticintcountofweekends(DateTime开始,DateTime结束){
返回CountDays(星期六,开始,结束)+CountDays(星期天,星期日,开始,结束);
}
公共静态int CountDays(DayOfWeek天、DateTime开始、DateTime结束)
{
TimeSpan ts=end-start;//总持续时间
int count=(int)Math.Floor(ts.TotalDays/7);//整周数
剩余天数=(int)(ts.TotalDays%7);//剩余天数
int sinceLastDay=end.DayOfWeek-day;//自上一个[天]起的天数
如果(SincellastDay<0)SincellastDay+=7;//调整自上一[天]起的负天数
//如果超过偶数周的天数大于或等于自上一[天]起的天数,则也要计算这一天。
如果(余数>=sincellastday)计数++;
返回计数;
}

要使这项工作正常进行,您需要改变以下几点:

  • 因为我们希望以十进制形式返回周末天数,所以我们需要将返回类型更改为表示该值的类型,例如
    double
  • 然后,当我们计算我们的天数时,我们需要用小时数除以24得到一天的分数。在下面的示例中,我更进一步,根据
    滴答声的数量而不是
    小时数来计算天数的分数
  • 最后,当我们添加天时,我们应该只使用结果的
    日期
    部分,以便将时间设置为午夜,但最后一天除外,我们希望使用指定的时间
  • 例如:

    public static double GetWeekendDaysCount(DateTime start, DateTime end)
    {
        if (start == end) return 0;
    
        if (start > end)
        {
            DateTime temp = start;
            start = end;
            end = temp;
        }
    
        double weekendDays = 0;
        var current = start;
    
        // To be super accurate, we can calculate based on Ticks instead of hours
        var ticksInADay = (double)TimeSpan.FromDays(1).Ticks;
    
        while (current <= end)
        {
            if (current.DayOfWeek == DayOfWeek.Saturday || 
                current.DayOfWeek == DayOfWeek.Sunday)
            {
                // If the time is midnight, count it as one day,
                // otherwise add a fraction of a day
                weekendDays += current.TimeOfDay > TimeSpan.Zero
                    ? current.TimeOfDay.Ticks / ticksInADay
                    : 1;
            }
    
            // Add a day and set the time to midnight by using 'Date'
            current = current.AddDays(1).Date;
    
                // Unless we're on the last day, then we want 
                // to use the TimeOfDay that was specified
            if (current == end.Date) current = end;
        }
    
        return weekendDays;
    }
    
    public静态双GetWeekendDaysCount(日期时间开始,日期时间结束)
    {
    if(start==end)返回0;
    如果(开始>结束)
    {
    日期时间温度=开始;
    开始=结束;
    结束=温度;
    }
    双星期日=0;
    无功电流=启动;
    //为了超级精确,我们可以根据滴答声而不是小时来计算
    var ticksInADay=(双)TimeSpan.FromDays(1).Ticks;
    while(当前时间跨度为0
    ?当前.TimeOfDay.Ticks/ticksnaday
    : 1;
    }
    //添加一天并使用“日期”将时间设置为午夜
    当前=当前.AddDays(1).日期;
    //除非我们在最后一天,否则我们要
    //使用指定的TimeOfDay
    如果(当前==结束日期)当前=结束;
    }
    周末返回;
    }
    
    .AddDays
    不会更改
    TimeOfDay
    ,因此
    testDate.Minute
    将始终与
    startDate.Minute
    相同。此外,您的方法返回
    int
    ,因此它不会返回类似
    1.5
    的十进制值