【问题标题】:How to calculate WeekEnds in minutes inbetween the two given dates by considering the time also如何通过考虑时间来计算两个给定日期之间的周末(以分钟为单位)
【发布时间】:2019-11-08 16:53:34
【问题描述】:

我有两个字段 startdate 和 enddate ,我需要计算这两个日期之间有多少个周末并以分钟为单位显示。例如,开始日期为 01/11/2019 00:00:00,结束日期为 03/11/2019 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;
    }

将输出显示为周末的 2 天,而不是日期之间的 1.5 天。请建议我如何实现这一目标

【问题讨论】:

  • .AddDays 不会更改TimeOfDay,因此testDate.Minute 将始终与startDate.Minute 相同。
  • 另外,你的方法返回一个int,所以它永远不会返回像1.5这样的十进制值

标签: c#


【解决方案1】:

如果我理解正确,weekends 是指saturdaysunday

我使用此代码计算两个日期之间存在多少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;
}

Reference

【讨论】:

    【解决方案2】:

    你应该改变一些事情来完成这项工作:

    1. 由于我们希望以小数形式返回周末天数,因此我们需要将返回类型更改为表示该值的类型,例如 double
    2. 然后,当我们计算我们的天数时,我们需要通过将小时除以 24 来获得一天的分数。在下面的示例中,我更进一步,根据Ticks 的数量计算天数的分数而不是Hours
    3. 最后,当我们添加天数时,我们应该只使用结果的 Date 部分,以便将时间设置为午夜,除了最后一天,我们想要使用指定的时间。李>

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多