c# – 获取时间段之间的小时数

我在一个场景中运行,我需要找到当前时段的总小时数.我有一个时间和TimeOut日期时间,我希望得到员工在第二天晚上10点到凌晨4点之间工作的时间.我如何获得工作小时的输出.

我创建了一个像这样的扩展方法:

 public static decimal GetNightDifferentialValue(this DailyTime dtr, Employee201 employee, PayrollSettings settings, IEnumerable<Holidays> holidays)
    {
        //know if the time out is greater than 10pm of the dtr
        //07-26-2016 14:00 - 07-27-2016 03:00
        //if time out i
        var days = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
            .Select(i => dtr.TimeIn.AddHours(i))
            .Where(date => !(date.Hour >= 22)).Count();
        return days* employee.Rate;

    }

我的问题是在Where方法中如何过滤仅属于我的类别的小时数

最佳答案

 public static decimal GetNightDifferentialValue(this DailyTime dtr, Employee201 employee, PayrollSettings settings, IEnumerable<Holidays> holidays)
    {
        //know if the time out is greater than 10pm of the dtr
        //07-26-2016 14:00 - 07-27-2016 03:00
        //if time out i
        DateTime dayIn10pm = new DateTime(dtr.TimeIn.Year, dtr.TimeIn.Month, dtr.TimeIn.Day, 22, 0, 0);
        DateTime dayAfter04am = dayIn10pm.Add(new TimeSpan(6,0,0));

        var hours = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
            .Select(i => dtr.TimeIn.AddHours(i))
            .Where(date => (date > dayIn10pm && date <= dayAfter04am)).Count();
        return hours;

    }
点赞